解决方案:只需在CallByName语句中将值括在Value旁边即可强制对其进行评估。归功于罗里。干杯!
我正在制作一个通用函数,将我所有类中一个变量的所有值设置为给定值。它看起来像:
Sub MobClassSetAll(TargetData As String, Value As Variant)
For each MobClass in MobCollection
CallByName MobClass, TargetData, vbLet, Value
Next MobClass
End Sub
MobCollection是我所有类似Mob类的集合。
问题是CallByName
。当Value
初始化为Variant
时,其第四个参数(Args)会导致类型不匹配。我已经严格测试了这个,甚至发了一篇关于它的帖子here,但我还没有得到答案。
所以我的新问题是:
如何更改多个相似类中相同变量的值,其中要更改的变量和新值传递给函数(不使用CallByName
)?
编辑:重载该功能会成为可能的解决方案吗?刚刚在C ++课堂上学到了它,我觉得它很可能。
答案 0 :(得分:1)
编辑,将所需的帮助类减少到仅包含所有" main" class 共享属性
由于您已经在使用课程,因此您应该继续使用这样一个强大的功能
我的理解是你想要为同一个类的所有对象更改一些属性,那么最好的解决方案是将属性作为类本身,这将允许你一次性更改它们
当然,几乎总是与课程一起,准备工作有点长,但奖励是值得的努力
所以
共享属性类
设置Class
以保存所有要在所有Mob Class实例之间共享的属性
让我们在MobShareds
之后命名,其代码只是:
Public prop1 As Variant ' rename "prop1" with whatever name you need
Public prop2 As Variant ' ""
Public prop3 As Variant ' ""
Public prop4 As Variant ' ""
Mob Class
让您的Mob
类利用共享属性对象,因此其代码将为
Public props As MobShareds
现在在您的主要代码中,您必须
实例化一个对象以保存共享属性
Dim MobClassShareds As New MobShareds
设置每个新的Mob
实例"共享属性"属性为同一个对象
MobCollection.Add New Mob
Set MobCollection(MobCollection.Count).props = MobClassShareds ' set currently added 'Mob' object "shared" properties to the object that holds them
MobCollection.Add New Mob
Set MobCollection(MobCollection.Count).props = MobClassShareds ' set currently added 'Mob' object "shared" properties to the object that holds them
MobCollection.Add New Mob
Set MobCollection(MobCollection.Count).props = MobClassShareds ' set currently added 'Mob' object "shared" properties to the object that holds them
现在您已准备好使用一个语句在所有Mob
个对象中更改任何所需的共享属性
MobClassShareds.prop1 = "a" ' this will set ALL 'Mob' objects property 1 value to "a"
MobClassShared2.Value = 1 ' this will set ALL 'Mob' objects property 2 value to 1
用于测试以上内容的工作代码:
Mob
上课
Public props As MobShareds
MobShareds
上课
Public prop1 As Variant
Public prop2 As Variant
Public prop3 As Variant
Public prop4 As Variant
主要代码
Option Explicit
Sub main()
Dim MobClass As Mob
Dim MobCollection As New Collection
'-------------------
' declare and instantiate a variable to hold Mob class shared properties
Dim MobClassShareds As MobShareds
Set MobClassShareds = New MobShareds
'---------------------
' fill your collection of 'Mob' objects and set their "shared properties" property to the same 'MobClassShareds' object
MobCollection.Add New Mob
Set MobCollection(MobCollection.Count).props = MobClassShareds ' set currently added 'Mob' object "shared" properties to the object that holds them
MobCollection.Add New Mob
Set MobCollection(MobCollection.Count).props = MobClassShareds ' set currently added 'Mob' object "shared" properties to the object that holds them
MobCollection.Add New Mob
Set MobCollection(MobCollection.Count).props = MobClassShareds ' set currently added 'Mob' object "shared" properties to the object that holds them
'---------
' set shared properties for ALL classes to some string value
MobClassShareds.prop1 = "a" ' this will set ALL 'Mob' objects property 1 value to "a"
MobClassShareds.prop2 = "b" ' this will set ALL 'Mob' objects property 2 value to "b"
MobClassShareds.prop3 = 1 ' this will set ALL 'Mob' objects property 3 value to 1
MobClassShareds.prop4 = 2 ' this will set ALL 'Mob' objects property 4 value to 2
'check what stated right above ist true...
For Each MobClass In MobCollection
Debug.Print MobClass.props.prop1
Debug.Print MobClass.props.prop2
Debug.Print MobClass.props.prop3
Debug.Print MobClass.props.prop4
Next MobClass
'---------
' set shared properties for ALL classes to some numeric value
MobClassShareds.prop1 = 1 ' this will set ALL 'Mob' objects property 1 value to 1
MobClassShareds.prop2 = 2 ' this will set ALL 'Mob' objects property 2 value to 2
MobClassShareds.prop3 = "a" ' this will set ALL 'Mob' objects property 3 value to "a"
MobClassShareds.prop4 = "b" ' this will set ALL 'Mob' objects property 4 value to "b"
'...
'check what stated right above ist true...
For Each MobClass In MobCollection
Debug.Print MobClass.props.prop1
Debug.Print MobClass.props.prop2
Debug.Print MobClass.props.prop3
Debug.Print MobClass.props.prop4
Next MobClass
End Sub