5

下午好,

我想知道在 VBA 语言中是否有可能在类的初始化时将参数传递给类,就像在 Java 等面向对象语言中所做的那样,您可以在其中创建参数化构造函数。事件“Class_Initialize ()”不允许我输入参数。我怎么解决这个问题?

一切顺利。

4

2 回答 2

7

最接近的替代方案是工厂模式;

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")
于 2013-08-05T19:13:08.670 回答
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
于 2013-08-05T19:12:50.313 回答