Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21238

[RESOLVED] Custom Classes - accesing private variables directly

$
0
0
Hello

I have a general best practices type question.

I am wondering if its ok to assign values to a private pVariable directly, when I have public and private functions that will edit these values and pVariable is not supplied as an argument to these functions.

For instance, I am creating a FileSearch Class that holds a private variable pCurrentSearchHit

This property is meant to be read only in its public interface, and so it seems I have two options.

1. Have the methods that write to this variable access the pCurrentSearchHit directly.
Code:

Private Function fReassignCurrentHit() As String

    pCurrentSearchHit = pFilePath & pFileName & pFileExtension

End Function

2. Create a Private Property Let procedure to assign the variable.
Code:

Private Function fReassignCurrentHit() As String

    Me.CurrentSearchHit = Me.FilePath & Me.FileName & Me.FileExtension

End Function

It seems that writing to the private variable is more efficient, although I am really not sure how much of a performance hit filtering the property through a let or get procedure incurs. For instance, what if a function needs to reference that variable several times?

I realize that filtering through a Private Property Let procedure provides more quality checks, but may not be necessary as the public properties which ultimately define this variable have several checks in place.

Also, when a Private Let procedure is in place the client code does not recognize the property as read only, and instead throws a data member not found error when the client tries to directly assign .CurrentSearchHit a value.


Is there a right/wrong way to do this, and do I gain anything significant by accessing the Private pCurrentSearchHit directly?

Viewing all articles
Browse latest Browse all 21238

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>