将参数传递给 VBA 中的构造函数 [英] Pass arguments to Constructor in VBA

查看:49
本文介绍了将参数传递给 VBA 中的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何构造将参数直接传递给自己的类的对象?

How can you construct objects passing arguments directly to your own classes?

像这样:

Dim this_employee as Employee
Set this_employee = new Employee(name:="Johnny", age:=69)

无法做到这一点非常烦人,最终您只能找到肮脏的解决方案来解决这个问题.

Not being able to do this is very annoying, and you end up with dirty solutions to work this around.

推荐答案

这是我最近使用的一个小技巧,并带来了不错的效果.想和经常和VBA打架的人分享一下.

Here's a little trick I'm using lately and brings good results. I would like to share with those who have to fight often with VBA.

1.- 在您的每个自定义类中实现一个公共启动子程序.我在所有课程中都将其称为 InitiateProperties.此方法必须接受您要发送给构造函数的参数.

1.- Implement a public initiation subroutine in each of your custom classes. I call it InitiateProperties throughout all my classes. This method has to accept the arguments you would like to send to the constructor.

2.- 创建一个名为 factory 的模块,并创建一个带有Create"字样的公共函数,加上与类相同的名称,以及与构造函数所需的相同传入参数.此函数必须实例化您的类,并调用第 (1) 点中解释的启动子例程,传递接收到的参数.最终返回实例化和启动的方法.

2.- Create a module called factory, and create a public function with the word "Create" plus the same name as the class, and the same incoming arguments as the constructor needs. This function has to instantiate your class, and call the initiation subroutine explained in point (1), passing the received arguments. Finally returned the instantiated and initiated method.

示例:

假设我们有自定义类 Employee.如前面的例子,is 必须使用名称和年龄进行实例化.

Let's say we have the custom class Employee. As the previous example, is has to be instantiated with name and age.

这是 InitiateProperties 方法.m_name 和 m_age 是我们要设置的私有属性.

This is the InitiateProperties method. m_name and m_age are our private properties to be set.

Public Sub InitiateProperties(name as String, age as Integer)

    m_name = name
    m_age = age

End Sub

现在在工厂模块中:

Public Function CreateEmployee(name as String, age as Integer) as Employee

    Dim employee_obj As Employee
    Set employee_obj = new Employee

    employee_obj.InitiateProperties name:=name, age:=age
    set CreateEmployee = employee_obj

End Function

最后当你想实例化一个员工时

And finally when you want to instantiate an employee

Dim this_employee as Employee
Set this_employee = factory.CreateEmployee(name:="Johnny", age:=89)

当您有多个类时特别有用.只需在模块工厂中为每个函数放置一个函数,并通过调用 factory.CreateClassA(arguments)factory.CreateClassB(other_arguments) 等进行实例化

Especially useful when you have several classes. Just place a function for each in the module factory and instantiate just by calling factory.CreateClassA(arguments), factory.CreateClassB(other_arguments), etc.

正如 stenci 指出的那样,您可以通过避免在构造函数中创建局部变量来使用更简洁的语法来做同样的事情.例如 CreateEmployee 函数可以这样写:

As stenci pointed out, you can do the same thing with a terser syntax by avoiding to create a local variable in the constructor functions. For instance the CreateEmployee function could be written like this:

Public Function CreateEmployee(name as String, age as Integer) as Employee

    Set CreateEmployee = new Employee
    CreateEmployee.InitiateProperties name:=name, age:=age

End Function

哪个更好.

这篇关于将参数传递给 VBA 中的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆