Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 952247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:51:47+00:00 2026-05-15T23:51:47+00:00

I am writing a C#.NET application. I have a form. When the form is

  • 0

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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:51:47+00:00Added an answer on May 15, 2026 at 11:51 pm

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an .NET 3.5 web application that is using LinqToSql for the basic
I'm writing an ASP.NET 3.5 web application. There is a rather complex input form
I'm currently writing a C#-class in my ASP.NET (3.5) application to handle all database-queries.
I'm writing a Windows Form Application, that makes extensive use of datagridview and others
I'm writing an ASP.NET MVC application and trying to use a RESX file to
I'm writing this question from the standpoint of an ASP.NET application. However I realize
I'm writing a small application in VB.NET and I would like some of the
I'm writing a Windows Forms Application in C#.NET On startup, the application displays a
I'm writing (in C# with .NET 3.5) an administrative application which will poll multiple
My task is to create an ASP.NET application with a piece of code which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.