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.
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.
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.
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.
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")
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
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