Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameters at Class' Initialization in VBA

Tags:

vba

good afternoon,

I wonder if it is possible, in the VBA language, to pass parameters to a class on it's initialization time, as done in object-oriented languages ​​such as Java, where you can create parameterized constructors. the event "Class_Initialize ()" does not allow me to enter parameters. how can I solve this problem?

all the best.

like image 942
Thor My Avatar asked Aug 05 '13 18:08

Thor My


People also ask

How do you initialize a class in VBA?

The Initialize event occurs when you: Create a new instance of a class directly by using the New keyword with the Set statement. Dimension an object variable using the New keyword and creating an instance of a class indirectly by setting or returning a property or applying a method defined in the class module.

How do you pass a parameter to a function in VBA?

VBA allows you to pass variables into subroutines and functions in two ways. You can specify either ByVal or ByRef for each of the variables that are passed in. The ByVal and ByRef distinction for subroutine and function parameters is very important to make. In VBA all objects are passed by reference.

What is the difference between let and set in VBA?

Let statements can be used to assign one record variable to another only when both variables are of the same user-defined type. Use the LSet statement to assign record variables of different user-defined types. Use the Set statement to assign object references to variables.


2 Answers

The closest alternative is a factory pattern;

public function CreateMyClass(i as integer, str as string) As cMyClass
    Set CreateMyClass = New cMyClass
    '// a method within class to accept constructor-like args;
    CreateMyClass.ctor i, str
    '// alternatively setup via properties
end function

...

dim myClass As cMyClass
set myClass = CreateMyClass(123, "Hello")
like image 113
Alex K. Avatar answered Sep 26 '22 23:09

Alex K.


make your own on to wrap around that?

Sub new_myClass(str1 as String, int1 as Integer) As myClass
  Dim mc As myClass
  mc.int_attribute = int1
  mc.str_attribute = str1
  '...
  return mc
End Sub
like image 28
Mr.Monshaw Avatar answered Sep 25 '22 23:09

Mr.Monshaw