Background:
这是一个后续问题this我最近问的问题是如何返回数组Class
模块属性直接来自Dictionary
item.
我现在已经尝试过与Property Let
and Property Get
填充一个Private Array
来填充Dictionary
和。但是,运行一些测试时我遇到了Error 438
.
Code:
Imagine 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
Problem:
错误 438 将发生在dict(keyx) = lst
并告诉我该对象(字典)不支持此Property
or Method
。这个问题对我来说似乎很棘手lst
对象似乎没有问题dict.Add x, lst
。事实上,改变方法Item
通过其Key
这似乎是一个非常common实践。
Question:
而类似的东西Dict.Add x, "Hello"
进而Dict(x) = "Hello World"
似乎有效。代码在使用时出错Class
第二种方法中的对象。有谁知道为什么,如果是的话,如何处理这个问题?
谢谢你,
联合病毒