7

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?

1
  • 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. Commented Aug 5, 2013 at 19:08

3 Answers 3

10

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") 
Sign up to request clarification or add additional context in comments.

Comments

3

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

I'm confused. How is it possible for a 'Sub' to return 'As myClass' ?
-1

Now it is possible, the constructor is called "sub new" and you can provide parameters.

1 Comment

Providing some example code would make this answer much more helpful. Please edit your question to include more detail.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.