Using statement and office interop

  • Thread starter Thread starter Robert Bravery
  • Start date Start date
R

Robert Bravery

Hi all,

Being new to C# and .net I often don't know how to use things.
I have created an app that imports excel data, it works well, with methods
to open excel, extract the data and close excel.
Now a colege of mine suggested useinf the usingstatement, so that the office
object is dispossed/destroyued correctly, saying that it prevents
memoryleaks. Problem is I am having problems trying to achieve this, I keep
getting the error:
type used in a using statement must be implicitly convertible to
'System.IDisposable'

Which at thie early time makes no sense to me, can someone explain and
perhaps with some code I can follow

Thanks
Robert
 
Robert said:
Hi all,

Being new to C# and .net I often don't know how to use things.
I have created an app that imports excel data, it works well, with methods
to open excel, extract the data and close excel.
Now a colege of mine suggested useinf the usingstatement, so that the
office object is dispossed/destroyued correctly, saying that it prevents
memoryleaks. Problem is I am having problems trying to achieve this, I
keep getting the error:
type used in a using statement must be implicitly convertible to
'System.IDisposable'

Which at thie early time makes no sense to me, can someone explain and
perhaps with some code I can follow

Thanks
Robert

Hi Robert,

The using clause is designed to be used on types that implement the
IDisposable interface, i.e. what the error is saying. This is because the
using statement is basically a wrapper for the following code:

try
{
// Contents of using block
}
finally
{
<using-object>.Dispose();
}

Where <using-object> is the object that you are specifying in the using
statement. The using statement calls code within a try...finally block and
in the finally part calls the Dispose method of the object. So, in order
for the using statement to work, the object that you are specifying needs
to implement the IDisposable interface, i.e. exhibit a 'Dispose()' method.
 
HI Tom,

Thanks for the reply. Yes I think this helps.
SO I tried to find a dispose method for
Microsoft.Office.Interop.Excel.Application()
But could'nt, therefore cannot use this object witin the using statement as
previously asket.
So then shat would you say is an efficient ways of making sure that I have
all bases coverd with regards to closing excel and disposing of objects
This is what I got, am I moissing anyhting?

objBooks.Saved = true;
objBooks.Close(Missing.Value, Missing.Value, Missing.Value);
objApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(Range);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objSheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objApp);
objApp = null;
objBooks = null;
objSheet = null;
objSheets = null;
Range = null;
//force a garbage collection
System.GC.Collect();

Thanks
Robert
 

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

Back
Top