Class Method Can't Use Private Property of Same Class?

puzzlepiece87 Source

What am I misunderstanding about Private Properties in VBA classes? As a novice, I expected Private Properties to be accessible to other methods within the same class (module), but instead I get "Method or data member not found" when trying to compile.

Breaks as Private Property Let lngMarketID

Here's what I have in my standard module:

Option Explicit

Public Model As classModel

Set Model = New classModel

with this class module named classModel:

Option Explicit

Private plngMarketID As Long

'plngMarketID Properties
Public Property Get lngMarketID() As Long
    lngMarketID = plngMarketID
End Property
Private Property Let lngMarketID(ByVal lngMarketID As Long)
    plngMarketID = lngMarketID
End Property

Public Sub Setup()


End Sub

Private Sub SetuplngMarketID()

    Model.lngMarketID = CLng(DefaultLogicOptions.textboxMarketID.Value)

End Sub

Works as Public Property Let lngMarketID

The "Method or data member not found" highlights the .lngMarketID of the line in SetuplngMarketID. This compiles fine when I change Private Property Let lngMarketID to Public Property Let lngMarketID.



answered 10 months ago Erik Westwig #1

It's the "Model" in that line. Model is a global variable pointing to some specific instance of your class. From that object, only the public things are visible.

You want to refer to that from "inside" an arbitrary instance of your class, so just drop the Model prefix:

Private Sub SetuplngMarketID()

    lngMarketID = CLng(DefaultLogicOptions.textboxMarketID.Value)

End Sub

answered 10 months ago exSnake #2

You are calling "Model.lngMarketId", Model is the name of your variable of classModel object that isn't visible in the object it self.

you have to use "lngMarketId = Clng(something)"

comments powered by Disqus