Let Property passing argument

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I seem to be confused with regard to ByRef and ByVal.

Here, regardless of ByRef or ByVal, the result in the immediate window is 100. I though ByVal DOESN'T change things.

Code:
Option Explicit


    Dim pabc As Long


Sub Test()


    abc = 10
    
    Debug.Print pabc
    
End Sub


Property Let abc(ByVal a)


    a = 100
    
    pabc = a
    
End Property
 
Last edited:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
My mistake. I had forgotten that Property Get follows the normal rules (I never use Property Get with arguments hence my rustiness) but Property Let and Set always pass ByVal no matter what you specify.


My intuition agrees with you (re Let always ByVal) because if you created stubs using Insert -> Procedure -> Property, the VB editor automatically adds the keyword, ByVal to the Let Property (though I cannot prove similarly for the Set, as you can't use drop downs to create that).
 
Last edited:
Upvote 0
It's easy enough to demonstrate- try and replicate Mike's Property Get above as a Property Let. Then repeat using an object type and trying to set it to a different object in the property procedure. :)
 
Upvote 0
The more I think of it, the more I conclude that it's somewhat pointless to pass arguments to a Get Property, as it does nothing.

In Mike's example because Smith = "Smith" in the Get Property, it doesn't matter that a = "cat".
 
Last edited:
Upvote 0
There can be uses for it - for example you might have a property that returned either an array or a specific element from that array.
 
Upvote 0
It's easy enough to demonstrate- try and replicate Mike's Property Get above as a Property Let. Then repeat using an object type and trying to set it to a different object in the property procedure. :)

Couldn't replicate, can you please post an example?
 
Upvote 0
Class module:

Code:
Private msSmith As String
Property Let Smith(ByRef a As String)
    a = "some other string"
    msSmith = a

End Property
Property Get Smith() As String
    Smith = msSmith
End Property

Note that a is allegedly passed ByRef which should mean it is altered for the calling routine and thus the property and a should have the same value, but this will show that it is not in fact changed:

Code:
Sub test()
    Dim x As Variant
    x = "xxx"
    Dim aThing As New Class1
    aThing.Smith = x
    MsgBox aThing.Smith
    MsgBox x
End Sub
 
Upvote 0
From what I can see, regardless of ByRef or ByVal, the two messages are:

Someotherstring
xxx

If Let Properties behaved "normally", are you expecting:

ByRef

Someotherstring
Someotherstring


ByVal

Someotherstring
xxx


Originally x="xxx" but here:

Code:
a = "some other string"

it has changed and is reflected in the messagebox, as expected.

The last line:

Code:
MsgBox x

will always show xxx.
 
Last edited:
Upvote 0
If Let Properties behaved "normally", are you expecting:

ByRef

Someotherstring
Someotherstring


ByVal

Someotherstring
xxx

Yes, exactly. That's how you can tell it is being passed ByVal no matter what you specify.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

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