下午好,
我想知道在VBA语言中是否有可能在其初始化时将参数传递给类,就像在Java等面向对象语言中所做的那样,您可以在其中创建参数化构造函数。事件“Class_Initialize()”不允许我输入参数。我该如何解决这个问题?
一切顺利。答案 0 :(得分:8)
最接近的替代方案是工厂模式;
public function CreateMyClass(i as integer, str as string) As cMyClass
Set CreateMyClass = New cMyClass
'// a method within class to accept constructor-like args;
CreateMyClass.ctor i, str
'// alternatively setup via properties
end function
...
dim myClass As cMyClass
set myClass = CreateMyClass(123, "Hello")
答案 1 :(得分:2)
让自己开始绕过那个?
Sub new_myClass(str1 as String, int1 as Integer) As myClass
Dim mc As myClass
mc.int_attribute = int1
mc.str_attribute = str1
'...
return mc
End Sub