locked
Destroy The Static Object RRS feed

  • Question

  • hi men 
    if we want to destroy the static object and then create it what can I do ? 

    ______________
    Friday, October 2, 2009 9:12 AM

Answers

  • class A
    {
        static object A1;
    
        static void CreateA()
        {
        A1 = new TextBox();
        }
       
        static void DistroyA()
        {
        A1 = null;
        }
    
    }


    Best regards, Sergiu
    • Proposed as answer by Jai Mallesh Babu Friday, October 2, 2009 10:28 AM
    • Marked as answer by Bin-ze Zhao Tuesday, October 6, 2009 5:35 AM
    Friday, October 2, 2009 10:26 AM
  •     static object A1;
    
    

    This is just a static reference, not a static object.  Furthermore, you may reset a reference by assigning null to it, but this may not cause the object to be destroyed (garbage collected).
    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    • Marked as answer by Bin-ze Zhao Tuesday, October 6, 2009 5:35 AM
    Friday, October 2, 2009 4:01 PM

All replies

  • Hi Daniel,

    You cannot have a static object, you can simulate one with a singleton pattern, though. 

    If by static object you mean a static reference to an instance of an object, then destroy the object as normal and replace the reference with a new object reference.  If, on the other hand, you mean reload a static class, then you need to unload the application domain to get rid of it.
    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    Friday, October 2, 2009 9:45 AM
  • class A
    {
        static object A1;
    
        static void CreateA()
        {
        A1 = new TextBox();
        }
       
        static void DistroyA()
        {
        A1 = null;
        }
    
    }


    Best regards, Sergiu
    • Proposed as answer by Jai Mallesh Babu Friday, October 2, 2009 10:28 AM
    • Marked as answer by Bin-ze Zhao Tuesday, October 6, 2009 5:35 AM
    Friday, October 2, 2009 10:26 AM
  •     static object A1;
    
    

    This is just a static reference, not a static object.  Furthermore, you may reset a reference by assigning null to it, but this may not cause the object to be destroyed (garbage collected).
    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    • Marked as answer by Bin-ze Zhao Tuesday, October 6, 2009 5:35 AM
    Friday, October 2, 2009 4:01 PM
  • You can even "destroy" (set to null, or .Dispose() ) your static object in a non-static method or even outside the referenced class.

    Best regards, Sergiu
    Friday, October 2, 2009 5:02 PM
  • It sounds like you're trying to reset your static class, if this is the case the easiest way may be adding a Reset method which manually resets it for you.

    [Edit] Come to think of it, if you need to do this then it shouldn't be a static class. You've probably made it a static class just so you can easily reference it from everywhere, but that's not good design. A static class is meant to represent something that there can only ever be one of. If you want to destroy it and create a new one there's now more than one (even if you only need one at a time) and you shouldn't be using a static class.
    Friday, October 2, 2009 5:34 PM