By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,640 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,640 IT Pros & Developers. It's quick & easy.

Using statement and office interop

P: n/a
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
Jul 16 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Robert Bravery wrote:
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-objectis 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.

--
Hope this helps,
Tom Spink

Google first, ask later.
Jul 16 '06 #2

P: n/a
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.ReleaseComO bject(Range);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(objSheet);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(objSheets);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(objApp);
objApp = null;
objBooks = null;
objSheet = null;
objSheets = null;
Range = null;
//force a garbage collection
System.GC.Collect();

Thanks
Robert

"Tom Spink" <ts****@gmail.comwrote in message
news:O7**************@TK2MSFTNGP04.phx.gbl...
Robert Bravery wrote:
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-objectis 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.

--
Hope this helps,
Tom Spink

Google first, ask later.

Jul 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.