'VBA Error 438 when trying to get class object from collection
I have a class in vba called MyClass
Option Explicit
Private msg As String
Public Property Get Message()
Message = msg
End Property
Public Sub Init(ByVal str As String)
msg = str
End Sub
I want to populate collection with objects of that class so to test
Dim items As Collection
Dim Item As MyClass
Set items = New Collection
Set Item = New MyClass
Item.Init "test"
items.Add Item
Item = items.Item(1)
I not really speacilist in scripting i just need to do simple things so i dont understand why is it, but when i run the block above i get error:
Run time error 438 Object' doesn't support this property or method
I checked in the debugger and it happens when i do step over on this line:
Item = items.Item(1)
How do i properly insert class objects to collection and get them back so i can use them without this error?
Solution 1:[1]
To be valid, Item must be an object type consistent with the object being assigned to it.
The Dim, Private, Public, ReDim, and Static statements only declare a variable that refers to an object. No actual object is referred to until you use the Set statement to assign a specific object.
So, don't forget to use the Set statement in VBA when you assign an object reference in the code:
Dim items As Collection
Dim Item As MyClass
Set items = New Collection
Set Item = New MyClass
Item.Init "test"
items.Add Item
Set Item = items.Item(1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eugene Astafiev |
