VBA:用值初始化对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9840769/
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
VBA: Initialize object with values?
提问by user1283776
I want to be able to initialize a
我希望能够初始化一个
CArticle
having the following properties:
CArticle
具有以下特性:
Private pNumber As String
Private pQuantity As Double
with either empty, pre-defined or current values. How can I achieve this? I'm thinking something along the lines of:
带有空值、预定义值或当前值。我怎样才能做到这一点?我在想一些事情:
New empty CArticle
新空 CArticle
pNumber
pQuantity
New dummy CArticle
新假人 CArticle
pNumber
pQuantity = 99999
New init CArticle(number, quantity)
新初始化 CArticle(number, quantity)
pNumber = number
pQuantity = quantity
回答by ja72
It is a pain in the neck but this is the only way to do it.
这是颈部疼痛,但这是唯一的方法。
File CArticle
文件条
Option Explicit
Private pNumber As String
Private pQuantity As Double
Private Sub Class_Initialize()
pNumber = vbNullString
pQuantity = 0
End Sub
Public Sub InitializeWithValues(ByVal number As String, ByVal quantity As Double)
pNumber = number
pQuantity = quantity
End Sub
Public Sub InitializeDefaultValues()
pNumber = vbNullString
pQuantity = 99999
End Sub
and in the calling module
并在调用模块中
Dim art As New CArticle ' Initialize value to empty
art.InitializeWithValues "Bowtie", 100 ' and assign values
Set art = New CArticle ' Initialize values to empty
art.InitializeDefaultValues ' Initialize values to default
回答by ElderDelp
If anyone gets here by a search, as did I. I recommend looking instead at the answers in StackOverFlow: Pass arguments to Constructor in VBA
如果有人通过搜索来到这里,就像我一样。我建议您查看StackOverFlow中的答案:将参数传递给 VBA 中的构造函数
This is not my answer,it came from Bgusach. I am including it here because I see that a useful answer is more than just a link.
这不是我的答案,它来自Bgusach。我把它包括在这里是因为我看到一个有用的答案不仅仅是一个链接。
Pass arguments to Constructor in VBA
将参数传递给 VBA 中的构造函数
Here's a little trick I'musing lately and brings good results. Bgusachwould like to share with those who have to fight often with VBA.
这是我最近使用的一个小技巧,并带来了不错的效果。Bgusach想与那些经常与 VBA 斗争的人分享。
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.
1.-在每个自定义类中实现一个公共启动子程序。我在所有课程中都将其称为 InitiateProperties。此方法必须接受您要发送给构造函数的参数。
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.
2.-创建一个名为 factory 的模块,并创建一个带有“Create”一词的公共函数,加上与类相同的名称,以及与构造函数所需的相同传入参数。该函数必须实例化您的类,并调用第 (1) 点中解释的启动子例程,传递接收到的参数。最后返回实例化和启动的方法。
Example:
例子:
Let's say we have the custom class Employee. As the previous example, is has to be instantiated with name and age.
假设我们有自定义类 Employee。正如前面的例子, is 必须用 name 和 age 实例化。
This is the InitiateProperties method. m_name and m_age are our private properties to be set.
这是 InitiateProperties 方法。m_name 和 m_age 是我们要设置的私有属性。
Public Sub InitiateProperties(name as String, age as Integer)
m_name = name
m_age = age
End Sub
And now in the factory module:
现在在工厂模块中:
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)
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.
当您有多个类时特别有用。只需在模块工厂中为每个函数放置一个函数,并通过调用factory.CreateClassA(arguments)、factory.CreateClassB(other_arguments)等来实例化。
EDIT
编辑
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:
正如 stenci 指出的那样,您可以通过避免在构造函数中创建局部变量来使用更简洁的语法来做同样的事情。例如 CreateEmployee 函数可以这样写:
Public Function CreateEmployee(name as String, age as Integer) as Employee
Set CreateEmployee = new Employee
CreateEmployee.InitiateProperties name:=name, age:=age
End Function
Which is nicer.
哪个更好。