从 VBA 函数返回新类对象的“正确”方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7614842/
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
What is the "right" way to return a new class object from a VBA function?
提问by e.James
I am looking for the proper way to create and return a new class object in VBA.
我正在寻找在 VBA 中创建和返回新类对象的正确方法。
I am familiar with the following pattern for returning a new Typevariable (returned by value):
我熟悉以下返回新类型变量(按值返回)的模式:
Public Type Foo
x as Integer
y as Integer
End Type
Public Function NewFoo() as Foo
NewFoo.x = 4
NewFoo.y = 2
End Function
What would be the equivalent syntax for a new Classobject (returned by reference)?
新Class对象(通过引用返回)的等效语法是什么?
Public Function NewMyClass() As MyClass
''// ...?
End Function
回答by oberfreak
If you want to return an Object in VBA you have to set it to the Method name
如果你想在 VBA 中返回一个对象,你必须将它设置为方法名称
Public Function NewMyClass() As MyClass
Set NewMyClass = CreateObject("Some.MyClass");
End Function