views:

85

answers:

2

VB6 class modules have no parameterized constructors. What solution have you chosen for this? Using facory methods seems like the obvious choice, but surprise me!

A: 

How about using the available class initializer? This behaves like a parameterless constructor:

Private Sub Class_Initialize()
    ' do initialization here

End Sub
0xA3
The fact that it doesn't accept any parameters makes it pretty useless as a constructor. It really only lets you set up a few things, but not truly initialize the object's state.
derekerdmann
Sorry, my question was unclear. I meant how to solve the problem that there is only a parameterless constructor: Class_Initialize
Dabblernl
+3  A: 

I usually stick to factory methods, where I put the "constructors" for related classes in the same module (.BAS extension). Sadly, this is far from optimal since you can't really limit access to the normal object creation in VB6 - you just have to make a point of only creating your objects through the factory. What makes it worse is having to jump between the actual object and your factory method, since organization in the IDE itself is cumbersome at best.

derekerdmann
Or what about adding the factory method to the class itself? Putting the factory methods in a module makes them application specific.
Dabblernl
@Dabblernl - But then you'd have to make them the equivalent of Java's `public static`, right? VB6 doesn't have static methods.
derekerdmann
Well, no need for a static class. You would have to create an instance to access the factory method, but as you do not make use of the object's dependencies that would be acceptable IMHO
Dabblernl
@Dabblernl - Ah, I see. That actually sounds like a better solution, I'll have to try it out.
derekerdmann
+1. It's worth mentioning a special case. If your class is defined in another component (e.g. ActiveX DLL), you can limit access to normal object creation. Make the class PublicNotCreatable which forces the client to use your factory method. http://msdn.microsoft.com/en-us/library/aa242107(VS.60).aspx
MarkJ