Results 1 to 4 of 4

Thread: delete an instance of a class

  1. #1

    Thread Starter
    Lively Member druid's Avatar
    Join Date
    Jun 2006
    Posts
    81

    delete an instance of a class

    hi everybody, for example a have a class and have created many instances of it, my question is how can i delete a certain instance of that particular class? can anybody point me in the right direction for me to solve my problem? thanks

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: delete an instance of a class

    How did you name the instances of your class?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: delete an instance of a class

    You don't. If you don't want to use that instance anymore then you simply remove all references to it, e.g. assign null to a variable that refers to it. If the object contains resources that need to be released then you would implement the IDisposable interface in your class and then you can call Dispose on that instance to release its resources. Beyond that, it's up to the .NET garbage collector when the actual memory the object occupies gets reclaimed.

  4. #4
    Addicted Member
    Join Date
    Sep 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Re: delete an instance of a class

    how you 'delete' it depends on how and where you are storing it and if it implements IDisposable. If the only thing holding a reference to it is a list then
    Code:
    list.Remove(item);
    . if it's a field then
    Code:
    field=null;
    if it implements IDisposable and is being used only in the scope of a single method and methods that method calls then use a using statement. If it's being used in the scope of a single method and you want to ensure no other code in that method can see that variable name, put that section in it's own scope block inside your method with
    Code:
    {var temporaryItem=new x; /* other method calls involving temporaryItem */ }
    Last edited by MaslowB; Nov 24th, 2009 at 09:55 AM. Reason: added extra code tags
    vb.net and C# in 2008 .net 3.5
    ImaginaryDevelopment blog

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