CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 1999
    Posts
    6

    Memory management



    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

  2. #2
    Join Date
    Dec 1998
    Posts
    28

    Re: Memory management



    You have to use an array for that.

  3. #3
    Join Date
    Feb 1999
    Posts
    6

    Re: Memory management



    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++.

  4. #4
    Join Date
    Nov 1998
    Posts
    10

    Re: Memory management



    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



  5. #5
    Join Date
    Dec 1998
    Posts
    1

    Re: Memory management



    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured