?
Solved

Close a class

Posted on 2006-05-29
9
Medium Priority
?
262 Views
Last Modified: 2010-04-16
Hi Experts,

Here is my class:
public class MyClass
    {
        public MyClass()
        {
         }

        /// <summary>
        /// MyClass is closing
        /// </summary>
        public event EventHandler MyClassClosing;

        public void OnMyClassClosing(EventArgs e)
        {
            if (MyClassClosing != null)
                MyClassClosing(this, e);
        }
     }

I need to close the class programmatically. So when I decide to close the class, I raise the MyClassClosing event to inform the user and then I close the class.

The problem is that I don't know how to close my class.

Thanks in advance for your answers.
0
Comment
Question by:noulouk
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 5

Expert Comment

by:aaronfeng
ID: 16787089
Can you define "close my class"?  Not sure what you mean by it.

Cheers,

Aaron
http://aaronfeng.blogspot.com/
0
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16787096
how about using a destructor for your class? (maybe a syntax error... not tested or anything)

public delegate MyClassClosingEventHandler(object sender, System.EventArgs e)
public event MyClassClosingEventHandler MyClassClosing;

~ MyClass()
{
    //do whatever you would do for your class closing?
    if(MyClassClosing != null)
    {   MyClassClosing(this, e);
}
0
 
LVL 6

Assisted Solution

by:PoeticAudio
PoeticAudio earned 200 total points
ID: 16787102
oops already saw one syntax error (missing bracket) i'm sure there are more

~ MyClass()
{
    //do whatever you would do for your class closing?
    if(MyClassClosing != null)
    {   MyClassClosing(this, e)   };
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:noulouk
ID: 16787118
OK, but the runtime destructs my object. I need to destruct the object by myself.
0
 
LVL 5

Assisted Solution

by:aaronfeng
aaronfeng earned 200 total points
ID: 16787215
You can't destruct the object yourself for managed code.  What you can do is set the object you want to destruct to null then call the garbage collection yourself.

For example,

YourObject o = new YourObject();
o = null;
GC.Collect();

Then you can fire the event inside of the destructor.

Just to let you know, generally it's not a good idea to call garbage collection yourself since it is a performance hit.

Cheers,

Aaron
0
 
LVL 11

Assisted Solution

by:anyoneis
anyoneis earned 800 total points
ID: 16787498
You should implement the IDisposible interface. An example from VS2005 help is here: ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/M_System_IDisposable_Dispose.htm

Then, you can dispose of your object when you like. Or, it will automatically be disposed if you don't - when the garbage is taken out.

David
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 800 total points
ID: 16787503
The normal practice is to provide a Dispose method, which is called by the client code and means "I won't use this object any more". In the Dispose method should clear all resources you don't need and maybe inform the user of the class. There is even an interface, IDisposable, which defines how the Dispose method should look like. So your code would be:

public class MyClass : IDisposable
    {
        public MyClass()
        {
         }

        /// <summary>
        /// MyClass is closing
        /// </summary>
        public event EventHandler MyClassClosing;

        public void OnMyClassClosing(EventArgs e)
        {
            if (MyClassClosing != null)
                MyClassClosing(this, e);
        }

        public void Dispose()
       {
              // Do any cleaning you need

              OnMyClassClosing (EventArgs.Empty);
       }
     }


And outside you would use it like this:

MyClass m = new MyClass();
// Do something with m
m.Dispose();
m = null;
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 16787506
@anyoneis: Sorry for the repetition, I was writing my comment when you posted yours
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 16787630
@TheAvenger: No problemo - it happens and the two responses make for a more complete answer - hopefully one that works for noulouk! :-)

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question