1 Replies - 742 Views - Last Post: 15 March 2012 - 08:23 PM Rate Topic: -----

#1 adn258   User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 816
  • Joined: 31-August 11

Should You Ever Dispose Of A Class?

Posted 15 March 2012 - 08:15 PM

So I know this might seem like a dumb question but say you take the function below which disables all controls except one which I will need in my program ok this is below the Form1 : Form class and with that class or any class for that matter you can use
Form1 callfunc = new Form1();

but at that point you can call callfunc.dipose(); IS THAT NECESSARY and should you do that?
That also said you can just call the function in this case with
without calling an object instance Below is an example of the code I'm talking about. Which would be more correct, and why? Also should you ever dispose a class?
Thanks Guys

 //function for disabling all controls BUT the INSTALL button
        public void Func_DisableAllButInstall()
            foreach (Control c in this.Controls)
                if (c.Name != "btn_InstallService")
                    c.Enabled = false;


        private void Form1_Load(object sender, EventArgs e)


This post has been edited by adn258: 15 March 2012 - 08:15 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Should You Ever Dispose Of A Class?

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Should You Ever Dispose Of A Class?

Posted 15 March 2012 - 08:23 PM

Why don't you just disable them in designer as the default state when the form is created? You're doing that upon load, so it seems that's how you want them.

Having FormA make GUI choices over FormB is almost always wrong.
It violates every principal of black box coding. It makes them 'tightly bound'. As soon as someone makes a change in FormB it has the potential to break FormA's calls into it.

If it's the right way you are looking for then see FAQ # 3. (Click the SHOW button below)

As for calling .Dispose()... Should you ever do that? Yes. There are times when it is the right thing to do. At this point in your learning you probably won't find a need for quite some time. But there are times when you do call it to clean up unmanaged resources, or to make sure other clean-up activities are done in the right order.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

FAQ (Frequently Asked Questions - Updated Mar 2012

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1