Me in a Property

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,825
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
According to this article:

Code:
https://renenyffenegger.ch/notes/development/languages/VBA/language/classes/index

it states:

Code:
  ' Apparently, the »me« keyword cannot be used  ' in member subs and member functions …

So I experimented by adding Me in a Property.

This is in Class1:

Code:
Option Explicit

Private pName As String

Public Property Get Name() As String

    Name = pName

End Property

Public Property Let Name(ByVal vNewValue As String)

    pName = vNewValue

End Property

Private Sub Class_Initialize()

    Name = "Some name"

End Sub

This is in a standard module:

Code:
Option Explicit

Sub Start()

    Dim j As Class1
    Set j = New Class1

    MsgBox j.Name

End Sub

The above displays a message box with the string "Some name", as expected.

However, if the Let Property was changed to:

Code:
Public Property Get Name() As String

    Me.Name = pName

End Property

the message box displays an empty string.

Stepping into the code, I noticed when I get to this line (in the original):

Code:
MsgBox j.Name

it jumps to here:

Code:
Public Property Get Name() As String

    Name = pName

End Property

then the message box shows the correct string.

However, stepping through the second scenario, the code again hits the Get Property but immediately after, it goes to the Let Property before displaying the message box (and an empty string).

Why is that?

Thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
The value of your custom made "Name" property can be read due to the GET procedure (by retreiving the value from a private variable within the class module) and can be assigned with another value due to the LET procedure (by storing the new value in the private variable within the class module). "Me" always refers to the class module, so with the "Me" as a prefix, telling VBA to assign a value to Me.Name, you're enforcing to assign a new value to your own custom made property (within the same class), therefore launching the LET procedure.
 
Upvote 0
The value of your custom made "Name" property can be read due to the GET procedure (by retreiving the value from a private variable within the class module) and can be assigned with another value due to the LET procedure (by storing the new value in the private variable within the class module). "Me" always refers to the class module, so with the "Me" as a prefix, telling VBA to assign a value to Me.Name, you're enforcing to assign a new value to your own custom made property (within the same class), therefore launching the LET procedure.

Thanks for your explanation.
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,114,002
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top