我有一个超级简单的类,我称之为元组:
Private vals(1) As Integer
-----
Public Property Get x()
x = vals(0)
End Property
-----
Public Property Get y()
y = vals(1)
End Property
-----
Public Sub SetVals(x As Integer, y As Integer)
vals(0) = x
vals(1) = y
End Sub
我有一个字典,其中填充了这些元组的数组:
for x = whatever to whatever
Set t = New Tuple
t.SetVals somevalue, someothervalue
ReDim Preserve tupleArray(x)
Set tupleArray(x) = t
next x
theDictionary.Add someKey, tupleArray
我想稍后从字典中检索数组,但我正在努力解决它。这就是我尝试过的事情
Sub DoStuffWithDictionary(dict as object, index as Integer)
Dim tupleArray() as Tuple
' Error: "Property let procedure not defined and property get procedure did not return an object"
tupleArray = dict.Items(index)
' Error: can't assign to array
Set tupleArray = dict.Items(index)
' Error: same as above
Dim tupleArray as Object
' Error: same as above
Dim tupleArray as Variant
End Sub
在字典键上循环,它们都打印出来好了,我就无法进入数组。
答案 0 :(得分:1)
请参阅以下示例:
在每个字典键迭代中,您还需要循环内部数组。
元组课程:
Private vals(1) As Long
Public Property Get X() As Long
X = vals(0)
End Property
Public Property Get Y() As Long
Y = vals(1)
End Property
Public Sub SetVals(ByVal X As Long, ByVal Y As Long)
vals(0) = X
vals(1) = Y
End Sub
<强>测试强>
Sub TestTuple()
Dim d As Scripting.Dictionary
Dim t As Tuple
Dim arr() As Variant
Dim i As Long, ii As Long
Set d = New Scripting.Dictionary
For i = 1 To 3
For ii = 0 To 3
Set t = New Tuple
t.SetVals i + ii + 10, i + ii + 20
ReDim Preserve arr(ii)
Set arr(ii) = t
Next ii
d.Add CStr(i), arr
Erase arr
Next i
Dim Key As Variant
For Each Key In d.Keys
For i = 0 To UBound(d(Key))
Debug.Print "X: " & d(Key)(i).X, "Y: " & d(Key)(i).Y
Next i
Debug.Print ""
Next
End Sub
输出:
X: 11 Y: 21
X: 12 Y: 22
X: 13 Y: 23
X: 14 Y: 24
X: 12 Y: 22
X: 13 Y: 23
X: 14 Y: 24
X: 15 Y: 25
X: 13 Y: 23
X: 14 Y: 24
X: 15 Y: 25
X: 16 Y: 26
修改强>
如果您愿意,可以将数组声明为Tuple。见下文:
Sub Test2()
Dim arr() As Tuple
Dim t As Tuple
Dim i As Long
For i = 0 To 4
Set t = New Tuple
t.SetVals i + 10, i + 20
ReDim Preserve arr(i)
Set arr(i) = t
Next i
For i = 0 To UBound(arr)
Debug.Print arr(i).X, arr(i).Y
Next
End Sub
'10 20
'11 21
'12 22
'13 23
'14 24
答案 1 :(得分:0)
您应该可以使用密钥访问项目:
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
rng = ActiveSheet.Shapes("Label1").TopLeftCell.Address
Range("A1").Value = rng
End Sub