背景:
这是对this最近的问题的后续问题,我问过如何直接从Class
项目返回Dictionary
模块属性的数组。
我现在尝试使用Property Let
和Property Get
来填充Private Array
来填充Dictionary
的方法。但是,在运行一些测试时,我遇到了Error 438
。
代码:
将TstClass
想象成一个具有以下代码的类模块:
Private lst(0 To 2) As Variant
Public Property Let Add(ByVal i As Long,ByVal NewVal As Variant)
lst(i) = NewVal
End Property
Public Property Get Val(ByVal i As Long) As Variant
Val = lst(i)
End Property
Public Function Getarray() As Variant
Getarray = vals
End Function
然后此代码在模块中进行测试:
Sub Test()
Dim x As Long,arr As Variant,lst As Class1
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
For x = 1 To 3
Set lst = New Class1
lst.Add(0) = x
lst.Add(1) = x
lst.Add(2) = x
dict.Add x,lst
Next x
For x = 4 To 3 Step -1
If dict.Exists(x) = False Then
Set lst = New Class1
lst.Add(0) = x
lst.Add(1) = x
lst.Add(2) = x
dict.Add x,lst
Else
Set lst = dict(x)
lst.Add(1) = lst.Val(1) + 2
lst.Add(2) = lst.Val(2) + 2
dict(x) = lst '< Error 438 on this line
End If
Next x
For Each key In dict.keys
arr = dict(keys).Getarray
Next key
End Sub
问题:
错误438将发生在dict(keyx) = lst
上,并告诉我该对象(字典)不支持此Property
或Method
。由于lst
对象在dict.Add x,lst
上似乎不是问题,所以这个问题对我来说似乎是狡猾的。实际上,像这样通过Item
更改Key
的方法似乎是非常common的做法。
问题:
而类似Dict.Add x,"Hello"
然后Dict(x) = "Hello World"
的东西似乎起作用。在第二种方法中使用Class
对象时,代码出错。有谁知道为什么,如果可以的话,如何处理这个问题?
谢谢你, 合资企业