1

I'm trying to create a Sub which will take a value and a Class Property and set the Property for me.

In my Class (Class1) I have the following:

Option Explicit

Private vValue As String, vTest As String
Public Property Let Value(v As String)
    vValue = v
End Property
Public Property Get Value() As String
    Value = vValue
End Property
Public Property Let Test(v As String)
    vTest = v
End Property
Public Property Get Test() As String
    Test = vTest
End Property

Then I am calling it using the following Sub in Module1

Sub TestSetProperty()
    Dim cl As New Class1

    SetProperty "Test", cl.Value

    Debug.Print "Value:", cl.Value
    Debug.Print "Test:", cl.Test

End Sub

My SetProperty Sub contains

Sub SetProperty(v As String, ByRef prop)
    prop = v
End Sub

Now I'd expect this to pass the property to the SetProperty Sub and set the value giving:

Value:        Test
Test: 

in the immediate window, however instead it returns

Value:        
Test:         

How can I make this work as expected?

Tom
  • 9,725
  • 3
  • 31
  • 48

1 Answers1

1

The code you posted is trying to pass a method by reference. This is not possible in VBA. The closest you can get is the CallByName function

https://learn.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/callbyname-function

freeflow
  • 4,129
  • 3
  • 10
  • 18
  • And here is a link showing some [sample code](https://stackoverflow.com/a/5707956/5162073). – Brian M Stafford Apr 28 '20 at 12:15
  • `to pass a method by reference` - property, that is. They did [implement](https://stackoverflow.com/a/564564/11683) it in VB.NET. This answer is correct for VBA though. – GSerg Apr 28 '20 at 12:16
  • That's quite disappointing but thank you for the alternative - will accept it as answered – Tom Apr 28 '20 at 12:32
  • Was there a reason you were not able to use cl.Test=cl.Value – freeflow Apr 28 '20 at 13:13