vba VBA中类初始化时的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18065523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:34:25  来源:igfitidea点击:

Parameters at Class' Initialization in VBA

vba

提问by Thor My

good afternoon,

下午好,

I wonder if it is possible, in the VBA language, to pass parameters to a class on it's initialization time, as done in object-oriented languages ??such as Java, where you can create parameterized constructors. the event "Class_Initialize ()" does not allow me to enter parameters. how can I solve this problem?

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

all the best.

祝一切顺利。

回答by Alex K.

The closest alternative is a factory pattern;

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

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")

回答by Mr.Monshaw

make your own on to wrap around that?

让你自己来环绕它?

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