-1

When using a Class Module in VBA, how come a call to a Private Sub will fail when using Me in front of it, but a call to a Public Sub is OK?

For example, the code below (not a full Class Module, just a snippit) produces the error Method or data member not found or the line Call Me.SetupQuote.

However, if I make SetupQuote() a Public Sub all is fine. Surely when a Class Module is calling from within itself it shouldn't matter if the sub is public or private?

I realise I can just call a Private Sub without using Me, but I want to understand why VBA behaves in this way. Thanks.

Private Sub Class_Initialize()
    Me.QuoteID = 15
    Call Me.SetupQuote
End Sub

Private Sub SetupQuote()
    Set Quote = New classQuote
    Call Quote.SetQuote(Me.QuoteID)
End Sub
David Gard
  • 11,225
  • 36
  • 115
  • 227

1 Answers1

1

This won't be a very satisfying answer, but that's the way MS programmed the Me interface. It probably wasn't a specific choice someone made to only show publicly facing properties and methods. Clearly the better user experience would be to expose both private and public. Rather it was just a side-effect of how Me was implemented.

The Me interface probably says "determine what kind of object I'm in, create an instance of that object, and expose that object instance." That object instance will only show public stuff just like it would if you had created and object instance in another module.

There's no mechanism for exposing private stuff when 'Me' creates an instance any more than there is when you create an instance. If they wanted to do it, they would have to pass some flag into the class builder that indicated whether it was being built via the Me interface or something else. If the flag was set, expose the private stuff, otherwise don't. Even if they had thought of that, they probably wouldn't implement it. It's additional complexity and someone would have brought up the point that you can just type the procedure name.

I don't work for MS or have any special knowledge about this. It's just my guess about why an obviously useful feature isn't there.

Dick Kusleika
  • 32,673
  • 4
  • 52
  • 73
  • Thanks for the explanation. "It's just my guess about why an obviously useful feature isn't there." That says it all for me - it would be useful and it's a shame it's not their. Coming from a background of web programming (PHP), which relies of referencing `this` within the Class, it's just habit to try and use `Me` – David Gard Dec 23 '13 at 09:11