Click to See Complete Forum and Search --> : Memory management


Rick Okrasinski
November 18th, 1998, 10:51 AM
I am instaniating an object with the command "Set myobj As MyObj". Now I may have multiple instances of the object running at once when I close the App how do I destroy ALL the objects. I do "Set myobj = Nothing" on the unload function of the form. But if I have five or "n" instances of the object how do I get them all.


Thanks


Rick

Justin Decker
November 18th, 1998, 07:30 PM
You have to use an array for that.

Rick Okrasinski
November 19th, 1998, 11:07 AM
I appreciate the answer. But what type of array would that be? An array of objects, or just an array to keep track of when they are instantiated?? Can you create an array of objects in VB?? I know you can in C++.

DragonDigital
November 23rd, 1998, 05:59 AM
What about a array of the same type as the objects or a collection.

What ever you use, it would store all your objects as you create them.

Then so remove all of them, you simply cycle though them, removing them

Sridhar Krishnamoorthy
December 10th, 1998, 02:28 AM
Hi Rick,


The statement "Set myobj As MyObj" is just a declaration. An object of instance MyObj is created by the statement "Set myobj = New MyObj". This creates only one instance of the object. You do some operations using the object. Then without destroying the object (setting it to Nothing), you repeat the statement

"Set myobj = New MyObj" then the old instance is destroyed automatically and in the same memory location a new object is created. By this way, you can make any no. of instantiation. But remember, u need to destroy the last instance by saying "Set myobj = Nothing".


Well, if u need separate instances of the same object do as below.


Dim oMyObject(n) as MyObj ' Here 'n' can be any positive number.


Code to create 'n' instances of the object


For I = 1 To n

Set oMyObject ( I ) = New MyObj

Next I


Code to destroy 'n' instances of the object


For I = 1 To n

Set oMyObject ( I ) = Nothing

Next I


or


Erase oMyObject '' This one statement is equivalent to the above for..loop.


I hope this clears ur doubt.


Regards,

Sridhar.