0

I've written a class in VBA, with members and properties set up as follows:

Private mSomeVar As String

Public Property Get SomeVar() As String
    '...
End Property

Private Property Let SomeVar(value As String)
    '...
End Property

Which seems like pretty standard practice - read-only properties with private Let functions so the class itself can modify the value, and I can apply some logic (validation, etc) in the property body while doing so, whilst preventing anything outside the class from changing it.

The problem is, when I try to use the Private property from within the class, the VBE tells me that it cannot be found:

Private Sub SomeInternalMethod()
    'Compile error: Method or data member not found
    Me.SomeVar = "new value" 
End Sub

Why can't I use the class's own private properties from within the class itself? If they're invisible from both within and without, is it possible to use private properties at all?

Kai
  • 2,050
  • 8
  • 28
  • 46
  • 1
    possible duplicate of [Why can't the VBA Me keyword access private procedures in its own module?](http://stackoverflow.com/questions/1895633/why-cant-the-vba-me-keyword-access-private-procedures-in-its-own-module) – RubberDuck May 28 '14 at 11:10

2 Answers2

2

Found the problem in the Related questions. The Me keyword in VBA only exposes the Public interface of the class, not Private. That seems really obnoxious, but can solve the problem by removing the Me keyword (although then I potentially have to deal with shadowing where a method takes parameters with the same name as the property. More needless hungarian notation? Yay!)

Why do calls to Class Module Private Subs fail when using `Me`?

Why can't the VBA Me keyword access private procedures in its own module?

Community
  • 1
  • 1
Kai
  • 2,050
  • 8
  • 28
  • 46
0

Just don't use the Me object

Private Sub SomeInternalMethod()
    SomeVar = "new value" 
End Sub
z̫͋
  • 1,531
  • 10
  • 15