Return array of class properties from dictionary

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-14 10:26:17

问题


Background:

While recently trying to answer a question, I had myself thinking if it would be possible to return an array of class object properties directly from a dictionary item.

Code:

Imagine TstClass as a class module with the following code:

Public val1 As Variant
Public val2 As Variant
Public val3 As Variant

Then this code to test:

Sub Test()

Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim lst As TstClass, key As Variant, arr As Variant

For x = 1 To 3
    Set lst = New TstClass
    lst.val1 = "A" & x
    lst.val2 = "B" & x
    lst.val3 = "C" & x
    dict.Add x, lst
Next x

For Each key In dict
    arr = Array(dict(key).val1, dict(key).val2, dict(key).val3)
Next key

End Sub

Problem:

In the above case Array(dict(key).val1, dict(key).val2, dict(key).val3) will return the array just fine, but if we imagine val4-val50 it will become a lot more code. Either with Array(...) or writing it out line by line Debug.Print Dict(key).Valx.

Question:

Is it possible to return an array directly from the class object properties from the dictionary key? The most obvious thing to try for me was arr = Dict(key) hoping it would somehow recognize I needed all properties from the item. On top of that it's not allowed to declare Constants, Arrays, User Defined Types as Public, so something like Public Vals(0 to 2) won't work either.

Thank you, JvdV


回答1:


Not sure if this is exactly what you are after, but it is possible to make a method inside your class that would build an array of the properties you desire. You can then call that on each of the instances of the class to get the array details.

Class - named 'Example':

Public val1 As Variant
Public val2 As Variant
Public val3 As Variant

Public Function GetArray() As Variant
    GetArray = Array(Me.val1, Me.val2, Me.val3)
End Function

Client Code - In a Standard Module:

Sub SOExample()

    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    Dim lst As Example, arr As Variant, key As Variant

    For x = 1 To 3
        Set lst = New Example
        lst.val1 = "A" & x
        lst.val2 = "B" & x
        lst.val3 = "C" & x
        dict.Add x, lst
    Next

    For Each key In dict.keys
        arr = dict(key).GetArray
        Debug.Print Join(arr, ",")
    Next

End Sub

Output:

A1,B1,C1
A2,B2,C2
A3,B3,C3



回答2:


Taken inspiration form the answer by @RyanWildry, I figured out that, despite being unable to use a Public variant array, we could use a Private one. Therefor, I made it such that Property Let would assign the value to a specific index within the array.

Creating a Public Function to return the Private array variable would then return the array in one go.


Class module, named TstClass

Private vals(0 To 2) As Variant

Public Property Let val(ByVal x As Long, ByVal NewVal As Variant)
    vals(x) = NewVal
End Property

Public Function GetArray() As Variant
    GetArray = vals
End Function

Code under Module1

Sub Test()

Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim lst As TstClass, key As Variant, arr As Variant

For x = 1 To 3
    Set lst = New TstClass
    lst.val(0) = "A" & x
    lst.val(1) = "B" & x
    lst.val(2) = "C" & x
    dict.Add x, lst
Next x

For Each key In dict
    arr = dict(key).GetArray
Next key

End Sub

Another way would be to leave the idea of an Class module alone, and go with (for example) an ArrayList object:

Sub Test()

Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim lst As Object: Set lst = CreateObject("System.Collections.ArrayList")
Dim arr As Variant

For x = 1 To 3
    lst.Add "A" & x
    lst.Add "B" & x
    lst.Add "C" & x
    dict.Add x, lst.Toarray
    lst.Clear
Next x

For Each key In dict
    arr = dict(key)
Next key

End Sub

However, the latter approach would surpass the purpose of this question.

This is as close as I can get as to returning an array directly from a dictionary item. I'm happy to recieve constructive feedback or other/better ways as Class modules are rather new to me and this still doesn't answer the essence of the question on how to return an array of the properties directly.



来源:https://stackoverflow.com/questions/58838720/return-array-of-class-properties-from-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!