views:

78

answers:

2

I am writing a C#.NET application. I have a form. When the form is created I create an instance of a class. When I close the form, I want to dispose of the class so that the next time I open the form I can just create a fresh new instance of the class. So, in the form_Closing event I added code like this: classInstance = null; The problem is, for some reason, the next time I open the form the class is not equal to null but rather it is in the same state as it was right before I closed the form. Why is this happening?

EDIT: Adding Info:

myHandler is a field in the Form class. it looks like this:

private HSFW_Handler myHandler;

The Class that I am referring to is a singleton so I create it like this:

    public static HSFW_Handler GetInstance()
    {
        if (myHSFW == null)
        {
            myHSFW = new HSFW_Handler();
            return myHSFW;
        }
        else return myHSFW;
    }

I create an initial instance of it in the Form_Shown event

    private void SetupDialogForm_Shown(object sender, EventArgs e)
    {
        try
        {
            myHandler = HSFW_Handler.GetInstance();
            UpdateDisplay();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

The Form Closing looks like this...

private void SetupDialogForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        myHandler = null;
    }
A: 

Are you certain that the form_Closing event is being fired, so that you're closing the form and not hiding it?

If so, then search through the code looking for every time classInstance is set to something. You'll probably find the culprit in or around the constructor or another event such as Load or Shown.

Adam Ruth
Yes I am sure it's being fired. See the code that I added above.
Jordan S
+3  A: 

Let's look at this code:

public static HSFW_Handler GetInstance()
{
    if (myHSFW == null)
    {
        myHSFW = new HSFW_Handler();
        return myHSFW;
    }
    else return myHSFW;
}

And this:

private void SetupDialogForm_FormClosing(object sender, FormClosingEventArgs e)
{
    myHandler = null;
}

Notice anything?

You're setting myHandler to null; but this appears to be an instance-level member of SetupDialogForm. Or anyway, it's not the same as myHSFW, which is your static variable in the HSFW_Handler class.

When you do this...

myHandler = HSFW_Handler.GetInstance();

...you're making myHandler a reference to the same object pointed to by HSFW_Handler.myHSFW; but they're still two separate references. Setting one to null has no impact on the other.

The point of all this is that you need to actually change the value of myHSFW to null to get the behavior you seem to want.

I'd do it like this:

public class HSFW_Handler
{
    public static void DeleteInstance()
    {
        myHSFW = null;
    }
}

Then:

private void SetupDialogForm_FormClosing(object sender, FormClosingEventArgs e)
{
    HSFW_Handler.DeleteInstance();
}
Dan Tao
hmmm I see what you mean but I tried it and it still doesn't seem to be working...
Jordan S
@Jordan: What is in your `HSFW_Handler` constructor?
Dan Tao
Actually I just got it. Your solution worked, I just had to set one other thing to null as well. Thanks.
Jordan S