Me in a Property

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,416
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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
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.
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,416
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.
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
You're welcome :)
 

Watch MrExcel Video

Forum statistics

Threads
1,130,403
Messages
5,641,936
Members
417,247
Latest member
Chitaah

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
Top