The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

VB6 DLL Question

I need to keep track of the total number of a certain object that's been instantiated.  Since in VB6, it seems you can't have static data members, I created a global variable in a module called g_CarCount.  And a Car object that looks something like this: 

Option Explicit

Private m_Make As String
Private m_Model As String

'blah blah blah
Private Sub Class_Initialize()
    g_CarCount = g_CarCount + 1
    Debug.Print "Car count: " & g_CarCount
End Sub

But every time all Car objects go out of scope when running the exe, it forgets the count and goes back to 0.  What do VB6ers do in such cases?  (Doesn't forget when running in the IDE for some reason, though.)  Thannks.
Derp the VB6 Newbie
Thursday, June 28, 2007
The lifetime of a global variable in a module is the lifetime of the application. In the case of a COM DLL, this means as long as any object in the DLL is alive. I suspect what is happenning is that you are instantiating only Car objects, and when all of them are discarded, the module and its variables are also discarded. Load/unload behaviour in the VB IDE is slightly different, as you ave already discovered.
Raj Chaudhuri Send private email
Friday, June 29, 2007
+1 Raj.  You might try running it as an "ActiveX EXE", in which case once instantiated I think it will stay loaded.  You'll need a 'stub' 'Main()' to do that.
Friday, June 29, 2007
Of course, another alternative is to make sure you maintain at least ONE 'car' object in your application.  Then the DLL will never go completely out of scope.
Friday, June 29, 2007
Having a startup object of Sub Main won't do it alone.  Even a loaded hidden form will however.  Then the question is when do you ever want to unload?

Probably cleaner to persist the information to disk if it's really needed.  Use a global semaphore to detect when the first instance of your class is created, last instance terminated.
Some Guy
Friday, June 29, 2007
Try creating a separate factory class from which you get all your cars. Mark the class as GlobalMultiuse and store the number of instantiated objects in it. That class defintion could be:

private mCarCount as long

public CarFactory() As Car
  dim newCar as Car
  set newCar = new Car

  mCarCount = mCarCount + 1
  set CarFactory = newCar
end function

public GetNumberOfCars() as Long
  GetNumberOfCars = CarCount
end function

Then you create cars like this:

Dim newCar = CarFactory()
MsgBox GetNumberOfCars

As the class is GlobalMultiUse there in only a single instance and it will only go out of scope when your application closes.
JSD Send private email
Saturday, June 30, 2007

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz