I want to be able to initialize a
CArticle
having the following properties:
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
pNumber pQuantity
New dummy CArticle
pNumber pQuantity = 99999
New init CArticle(number, quantity)
pNumber = number pQuantity = quantity
To create an object of a named class by using an object initializer. Begin the declaration as if you planned to use a constructor. Type the keyword With , followed by an initialization list in braces. In the initialization list, include each property that you want to initialize and assign an initial value to it.
Initialise will only be reported once. From then on you are adding the same instance to the array many times. The correct way to initialise new instances objects is as @Doug has told you, Set ... = New ...
Objects can be initialized using new Object() , Object. create() , or using the literal notation (initializer notation). An object initializer is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ( {} ).
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
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
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.
Here's a little trick I'm using lately and brings good results. Bgusach would like to share with those who have to fight often with 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.
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.
Example:
Let's say we have the custom class Employee. As the previous example, is has to be instantiated with name and 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
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.
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
Which is nicer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With