Posts VB10 vs VB6: Private Class Variables

VB10 vs VB6: Private Class Variables

This is part of series of posts categorized under VB10 vs VB6.

"Private class variables are not private to the class instance."

This is another one of those "huh?" responses.  However, I'll go ahead and discuss it anyway.

As documented in the VB6 Programmers Guide on page 429,

"It's just as easy to create private data for a class; simply declare a variable Private, and it will be accessible only from code within the class module."

Private mstrMothersMaidenName As String

This is what I can only assume Karl is referring to with the statement that private class variables are not private to the class instance.  Based on this, let's look at VB10:

Class Class1
  Private m_mothersMaidenName As String
End Class

Class Class2
  Sub DoSomething()
    Dim c As New Class1
    c.m_mothersMaidenName = ""
  End Sub
End Class

I have to point out that this code will immediately draw your attention to it by draing a blue wavy line below it and in the Error List you'll see something similar to the following:

'Class1.m_mothersMaidenName' is not accessible in this context because it is 'Private'. 

At this point, you are not able compile the code until error has to be addressed.  Sure looks like the private variable is private to the class to me.


Not much to say, this one just isn't true. It was never true to the best of my knowledge so the nod, again, has to go to VB10.


Bill McCarthy (MVP) writes in : "I think this is referring to accessing private variables from *another* instance of the same class. Reason for its existence in .NET is two-fold:

  • first the addition of shared methods to a class: this is probably the heart of the technical reason why. 
  • it also comes in handy a LOT for operator overloading (equality operators), serialization etc."

Interesting.  I can almost see this as being a possible point of contention. However, just because you can "see" it doesn't mean you can "use" it.   Serialization almost deserves it's own conversation; however, there are cases where you might need access to "member variables".  In any case, when they are set to private they are, in fact, scoped as such.  I may have to give this more thought.

This post is licensed under CC BY 4.0 by the author.