【问题标题】:What is the "right" way to return a new class object from a VBA function?从 VBA 函数返回新类对象的“正确”方法是什么?
【发布时间】:2011-09-30 18:58:40
【问题描述】:

我正在寻找在 VBA 中创建和返回新类对象的正确方法。

我熟悉以下返回新类型变量(按值返回)的模式:

Public Type Foo
    x as Integer
    y as Integer
End Type

Public Function NewFoo() as Foo
    NewFoo.x = 4
    NewFoo.y = 2
End Function

新的 Class 对象(通过引用返回)的等效语法是什么?

Public Function NewMyClass() As MyClass
    ''// ...?
End Function

【问题讨论】:

    标签: class vba object creation


    【解决方案1】:

    如果你想在 VBA 中返回一个对象,你必须将它设置为方法名称

    Public Function NewMyClass() As MyClass
        Set NewMyClass = CreateObject("Some.MyClass");
    End Function
    

    【讨论】:

    • 太棒了。那我还能用NewMyclass.someVariable = someValue吗?
    • 据我所知,您可以将 NewMyclass 视为普通变量,因此您也应该能够将方法称为属性
    猜你喜欢
    • 2011-04-26
    • 1970-01-01
    • 2021-10-30
    • 2019-07-04
    • 1970-01-01
    • 2020-10-17
    • 2019-12-30
    • 2011-09-08
    • 1970-01-01
    相关资源
    最近更新 更多