VBA: Initialize object with values? VBA: Initialize object with values? vba vba

VBA: Initialize object with values?


It is a pain in the neck but this is the only way to do it.

File CArticle

Option ExplicitPrivate pNumber As StringPrivate pQuantity As DoublePrivate Sub Class_Initialize()    pNumber = vbNullString    pQuantity = 0End SubPublic Sub InitializeWithValues(ByVal number As String, ByVal quantity As Double)    pNumber = number    pQuantity = quantityEnd SubPublic Sub InitializeDefaultValues()    pNumber = vbNullString    pQuantity = 99999End Sub

and in the calling module

Dim art As New CArticle       ' Initialize value to emptyart.InitializeWithValues "Bowtie", 100     ' and assign valuesSet art = New CArticle        ' Initialize values to emptyart.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.

Pass arguments to Constructor in VBA

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 = ageEnd 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_objEnd Function

And finally when you want to instantiate an employee

Dim this_employee as EmployeeSet 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.

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:

Public Function CreateEmployee(name as String, age as Integer) as Employee    Set CreateEmployee = new Employee    CreateEmployee.InitiateProperties name:=name, age:=ageEnd Function

Which is nicer.