In C#, I know that I can overload the constructor for a class by specifying it in the body of the class:
public class MyClass()
{
public MyClass(String s) { ... }
}
This overrides the default constructor (which has no parameters) and forces the class to be initialized with parameter s.
I know that in VBA I can initialize my class with Private Sub Class_Initialize()
, but I don't know if there's a way to force my class to be initialized with parameters. Can this be done?
Best Solution
As Jtolle indicated, this is simply not possible in VBA/VB6. There is no perfect way to work around this but, what I personally do is create a Public/Friend sub call Initialize with the parameters I want (in VBA/VB6 you use "Optional" parameters for overloading) and then put a quick check in all exposed members of the class that throws an exception if you try to access them without running the initialize method. A basic example might look like this:
It's not great, but it's about as good as it's going to get with VBA/VB6.