objects in .NET

  • Thread starter Thread starter abcd
  • Start date Start date
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.

}
 
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.

}
 
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
 
Back
Top