0

I declare 2 ClassModules:

Class1

Public MyProp As Class2
Public Sub MyMethod(val As Class2)
    Set MyProp = val
    MsgBox MyProp.Foo
End Sub

Class2

Public Foo As String

Sheet1

Private Sub CommandButton1_Click()
    Dim a As Class1
    Dim b As Class2

    Set a = New Class1
    Set b = New Class2

    b.Foo = "Bar"

    a.MyMethod (b)

End Sub

When I call a.MyMethod (b), I get "Object doesn't support this property or method"

but if I write the code as following, everything works as expected:

Private Sub CommandButton1_Click()
    Dim a As Variant
    Dim b As Variant

    Set a = New Class1
    Set b = New Class2

    b.Foo = "Bar"

    a.MyMethod (b)

End Sub

What Am I doing wrong?

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Stefano Balzarotti
  • 1,760
  • 1
  • 18
  • 35
  • 1
    Wrong syntax: `a.MyMethod b` works. – Ron Rosenfeld Oct 19 '19 at 12:17
  • So if I declare the type I cant use parentheses? Sorry I am not VBA expert – Stefano Balzarotti Oct 19 '19 at 12:25
  • 1
    Parentheses are generally used when setting one value equal to another.You are not doing that when calling your class method. Another option would be using the `Call` method as in `Call a.MyMethod(b)`. With `Call`, the argument list needs to be in parentheses. – Ron Rosenfeld Oct 19 '19 at 12:36
  • 2
    https://stackoverflow.com/questions/5413765/what-are-the-rules-governing-usage-of-brackets-in-vba-function-calls – Tim Williams Oct 19 '19 at 16:49
  • `a.method(b)`passes b as byval argument instead of being a byref argument, but it shouldn't raise an error here. Instead, what is concerning me is `Set MyProp = val` wich cannot be done : you need to copy each variable from the class. In this case easily enough `MyProp.foo = val.foo`. Often VBA doesn't give the right error line (place a breakpoint on a.method(b), you'll see it goes there and errors later) – Patrick Lepelletier Oct 20 '19 at 07:13

0 Answers0