As title, is it possible to assign UDT as item of collection/dictionary?
I want to have array-like complex UDT structure and call it by key.
This is continuous question from my previous post: Is there a way to call array member by key instead of index?
Here is my attempt:
It shows error: "Compile error: Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound function".
Or... is it possible to make my own custom class that is similar with collection/dictionary object that can assign any types of UDT as item (not making one class for one UDT) and access it by key? (Maybe this can be question for another post). I actually have never make custom class yet, so its better if its possible without making class.
Thank you in advance.
I want to have array-like complex UDT structure and call it by key.
This is continuous question from my previous post: Is there a way to call array member by key instead of index?
Here is my attempt:
VBA Code:
Sub SampleSub()
Dim ArrayA As tSampleType
Dim ArrayAs() As tSampleType
Dim ArrayA_IDs As New Collection
Dim ColA As New Collection
Dim i As Long, nData As Long, Name As String, DataX As Double
'Store array
ReDim ArrayAs(99)
For i = 0 To 99
With ArrayAs(i)
.Name = "abc" & i
ArrayA_IDs.Add i, .Name
nData = 1000
ReDim .DataX(nData - 1), .DataY(nData - 1), .DataS(nData - 1)
'and so on
End With
Next i
'Calling array member by key
'Success but mouthful (having to have separate ArrayAs and ArrayA_IDs variable)
Name = "abc0"
i = ArrayA_IDs(Name)
DataX = ArrayAs(i).DataX(123)
'Attempt to make UDT as item of collection
For i = 0 To 99
ColA.Add ArrayAs(i), ArrayAs(i).Name
Next i
'Simple single line access to UDT by key
DataX = ColA(Name).DataX(123)
'And moving forward, I can bypass only ColA to other subs/functions, not a set of ArrayAs and Array_IDs
End Sub
It shows error: "Compile error: Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound function".
Or... is it possible to make my own custom class that is similar with collection/dictionary object that can assign any types of UDT as item (not making one class for one UDT) and access it by key? (Maybe this can be question for another post). I actually have never make custom class yet, so its better if its possible without making class.
Thank you in advance.