Is it possible to pass parameters to a class at 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?
- Unfortunately, you can not do such a thing. Only two step workaround could do the thing: 1st step- initialization, 2nd step- calling method or property which pass any parameter.Kazimierz Jawor– Kazimierz Jawor2013-08-05 19:08:55 +00:00Commented Aug 5, 2013 at 19:08
Add a comment |
3 Answers
Unfortunately you cannot have parameterized constructors, 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 CreateMyClass.Prop = str end function ... dim myClass As cMyClass set myClass = CreateMyClass(123, "Hello") Comments
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 1 Comment
Virtual Person
I'm confused. How is it possible for a 'Sub' to return 'As myClass' ?