Home » excel » excel vba – How to use a static array as class property in VBA

excel vba – How to use a static array as class property in VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a following property in MyClass2:

 Private MyClass1Array(1 To 4) As MyClass1

where MyClass1 is another class I have defined. I don’t know how to adress this property: it can’t be set public, so I wrote this in MyClass2 module:

Public Property Let SetMyClass1Array(i As Integer, c As MyClass1)
    MyClass1Array(i) = c
End Property

Public Property Get GetMyClass1Array(i As Integer) As MyClass1
    GetMyClass1Array = MyClass1Array(i)
End Property

But this also doesn’t work. How to write this properties correctly? Thanks!

How to&Answers:

When you work with Class-objects, you need to use Set as you handle object-references, not primitive data-type values.

Public Property Let SetMyClass1Array(i As Integer, c As MyClass1)
    Set MyClass1Array(i) = c
End Property

Public Property Get GetMyClass1Array(i As Integer) As MyClass1
    Set GetMyClass1Array = MyClass1Array(i)
End Property