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
2010-08-01 17:10:56
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
2010-08-01 17:13:35
Sorry, my question was unclear. I meant how to solve the problem that there is only a parameterless constructor: Class_Initialize
Dabblernl
2010-08-01 20:58:43
+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
2010-08-01 17:19:44
Or what about adding the factory method to the class itself? Putting the factory methods in a module makes them application specific.
Dabblernl
2010-08-01 21:00:19
@Dabblernl - But then you'd have to make them the equivalent of Java's `public static`, right? VB6 doesn't have static methods.
derekerdmann
2010-08-01 21:55:24
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
2010-08-01 22:23:53
@Dabblernl - Ah, I see. That actually sounds like a better solution, I'll have to try it out.
derekerdmann
2010-08-01 23:25:56
+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
2010-08-02 09:02:39