objects in .NET

A

abcd

I am novice .NET and C# user. I am mainly C++, COM develoeper

I have written my first C# Windows applicaiton where I am using my COM
component.

in one of the C# method I have instantiated the global variable as below

public xyz.abc obj

public form1()
{
obj = new xyz.abc;
}

private void form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{

//cleanup code
// here I want to delete the global object called "obj".
// How can I do that. How .NET handles this.

}
 
R

Richard Blewett [DevelopMentor]

You don't, in general, have to explicitly handkle this as .NET is a garbage collected environment and given recourse presure the .NET runetime will clean up the COM object itself.

You could call

System.Runtime.interopServices.Marshal.ReleaseComObject(obj);

in your event handler but if the form is the top level form then its not necessary as the Garbage Collector (GC) will take care of it for you

Rehards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I am novice .NET and C# user. I am mainly C++, COM develoeper

I have written my first C# Windows applicaiton where I am using my COM
component.

in one of the C# method I have instantiated the global variable as below

public xyz.abc obj

public form1()
{
obj = new xyz.abc;
}

private void form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{

//cleanup code
// here I want to delete the global object called "obj".
// How can I do that. How .NET handles this.

}
 
S

sadhu

You can't declare global variables in C#. Even otherwise, you don't
need to delete managed objects, the GC takes care of that. If the
managed objects holds some resource, you need to call Dispose(). Search
for the Dispose() pattern for more details.

Regards
Senthil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top