Let Property passing argument

tiredofit

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

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.
Property procedures always pass ByVal anyway. I don't really see how that is relevant here though - the a variable is local to the property procedure. The original input was a literal value which is, unsurprisingly, unchanged. ;)
 
Upvote 0
"Property procedures always passes ByVal"

Then I'm further confused!

Look at this code:

Code:
http://www.informit.com/articles/article.aspx?p=1346864

which states:

Code:
Option Explicit

Public Enum anlCellType
    anlCellTypeEmpty
    anlCellTypeLabel
    anlCellTypeConstant
    anlCellTypeFormula
End Enum

Private muCellType As anlCellType
Private mrngCell As Excel.Range

Property Set Cell(ByRef rngCell As Excel.Range)
    Set mrngCell = rngCell
End Property

Property Get Cell() As Excel.Range
    Set Cell = mrngCell
End Property

Property Get CellType() As anlCellType
    CellType = muCellType
End Property

Property Get DescriptiveCellType() As String
    Select Case muCellType
        Case anlCellTypeEmpty
            DescriptiveCellType = "Empty"
        Case anlCellTypeFormula
            DescriptiveCellType = "Formula"
        Case anlCellTypeConstant
            DescriptiveCellType = "Constant"
        Case anlCellTypeLabel
            DescriptiveCellType = "Label"
    End Select
End Property

Public Sub Analyze()
    If IsEmpty(mrngCell) Then
        muCellType = anlCellTypeEmpty
    ElseIf mrngCell.HasFormula Then
        muCellType = anlCellTypeFormula
    ElseIf IsNumeric(mrngCell.Formula) Then
        muCellType = anlCellTypeConstant
    Else
        muCellType = anlCellTypeLabel
    End If
End Sub

in particular this line:

Code:
Property Set Cell(ByRef rngCell As Excel.Range)
    Set mrngCell = rngCell
End Property

why does he put ByRef if Properties always pass ByVal?

BTW I'm NOT questioning your comment!
 
Last edited:
Upvote 0
I don't know - maybe he forgot. (It doesn't really make any difference there anyway.)
 
Upvote 0
I sometimes will specify a default (such as ByRef in property proceedures) just to remind myself, when I later edit, of what is happening.

Specifying defaults, instead of remembering them, is good practice IMO. No loss of functionality, but saving brain space. E.G. Split("cat dog fish", " ") rather than Split("cat dog fish")
 
Upvote 0
But specifying ByRef has no effect in a property procedure - the argument will be passed ByVal regardless.
 
Upvote 0
I sometimes will specify a default (such as ByRef in property proceedures) just to remind myself, when I later edit, of what is happening.

Specifying defaults, instead of remembering them, is good practice IMO. No loss of functionality, but saving brain space. E.G. Split("cat dog fish", " ") rather than Split("cat dog fish")

Except that the default with properties is ByVal, NOT ByRef, contrary to the usual rule.
 
Upvote 0
I put this code in a class module.
Code:
Property Get Smith(a As Variant) As String
    a = "cat"
    Smith = "smith"
End Property
And this in a normal module
Code:
Sub test()
    Dim x As Variant
    x = "xxx"
    Dim aThing As New Class1
    MsgBox aThing.Smith(x)
    MsgBox x
End Sub

The first message box returned "smith". The second returned "cat".

The default for an argument passed to Property Get is ByRef.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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