错误438对象不支持此属性或方法-带字典的类对象 [英] Error 438 Object doesn't support this property or method - Class Object with Dictionary
问题描述
背景:
这是关于这的后续问题,我问了一个有关如何返回Class
数组的问题模块属性直接来自Dictionary
项.
This is a follow-up question on this recent question I asked on how to return an array of Class
module properties directly from an Dictionary
item.
我现在已经尝试使用Property Let
和Property Get
来填充Private Array
来填充Dictionary
的方式.但是,在运行一些测试时,我遇到了Error 438
.
I have now tried my way to work with Property Let
and Property Get
to populate a Private Array
to populate the Dictionary
with. However, running some test I encountered an Error 438
.
代码:
将TstClass
想象为具有以下代码的类模块:
Imagine TstClass
as a class module with the following code:
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
然后此代码在模块中进行测试:
Then this code to test in a Module:
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
上似乎不是问题.实际上,像这样通过Key
更改Item
的方法似乎非常常见练习.
The error 438 will occur on dict(keyx) = lst
and tells me that the object (a Dictionary) doesn't support this Property
or Method
. The problem seems dodgy to me as the lst
Object didn't seem to be a problem on dict.Add x, lst
. As a matter of fact, the method to change an Item
through its Key
like this seems to be a very common practice.
问题:
类似Dict.Add x, "Hello"
然后Dict(x) = "Hello World"
的东西似乎起作用.在第二种方法中使用Class
对象时,代码出错.有谁知道为什么,如果可以的话,如何处理这个问题?
Whereas something like Dict.Add x, "Hello"
and then Dict(x) = "Hello World"
seems to work. The code errors out on using a Class
object in the second method. Does anybody know why and if so, how to deal with this problem?
谢谢你, 合资企业
推荐答案
Set dict(keyx) = lst
由于变量lst
指向对象,因此在这里需要Set
.
Since the variable lst
refers to an object, Set
is required here.
这篇关于错误438对象不支持此属性或方法-带字典的类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!