回避策は代わりにVariant
配列のDictionary
オブジェクトの使用が考えられます。
「test」を返すために、次の機能を使用しています
Sub main()
Dim myDict As Scripting.Dictionary
Dim key As Variant
Set myDict = GetDict '<--| get your "test" dictionary with "indexes" as 'keys' and "elements" as 'items'
For Each key In myDict.Keys '<--| iterate over keys (i.e. over your "indexes")
MsgBox key '<--| this will give you the "index"
MsgBox myDict(key) '<--| this will give you the "element"
Next key
End Sub
辞書真実で
Function GetDict() As Scripting.Dictionary
'function to return a test dictionary
Dim i As Long
Dim myDict As New Scripting.Dictionary
For i = 1 To 10
myDict.Add i, "string-" & CStr(i) '<--| use the 'key' as your "index" and the 'item' as your element
Next i
Set GetDict = myDict
End Function
、以下逆アプローチは、より多くのsimilaに見える可能性がありRあなたの最初のコードに以下の機能を使用している
Sub main()
Dim myDict As Scripting.Dictionary
Dim oElement As Variant
Set myDict = GetDict2 '<--| get your "test" dictionary with "elements" as 'keys' and "indexes" as 'items'
For Each oElement In myDict.Keys '<--| iterate over dictionary keys (i.e. over your "elements")
MsgBox myDict(oElement) '<--| this will give you the "index"
MsgBox oElement '<--| this will give you the "element"
Next oElement
End Sub
:
Function GetDict2() As Scripting.Dictionary
Dim i As Long
Dim myDict As New Scripting.Dictionary
For i = 1 To 10
myDict.Add "string-" & CStr(i), i '<--| use the 'key' as your "element" and the 'item' as your key
Next i
Set GetDict2 = myDict
End Function
が、それはこのように、おそらく彼らの一意に違反し、keys
としてあなたの「要素」を使用しての主な欠点を持っていると思います、連続した整数は常にこの要件を満たします。
バリアントが配列またはコレクションの場合にのみ、それぞれに対して実行できます。その純粋な配列の場合、何にもアクセスするためのプロパティはありません。索引に興味がある場合は、 'lbound'から' ubound'にループしてください。そのコレクションの場合、基礎となるオブジェクトが公開するすべてのプロパティにアクセスできます。完全に合意された – cyboashu