Cannot implicitely convert a Custom type to IDisposable Error
I'v开发者_如何学Goe got this piece of code:
try
{
using(conn)
{
conn.UpdateScheduledTaskGuid(taskID, taskGUID);
}
}
On the conn variable I'm getting the error "Cannot implicitly convert type DataProvider to System.IDisposable"
DataProvider is a custom class that someone created in this project I'm working in which is the type for the conn variable above.
I guess I need to have DataProvider implement IDisposable but I am not sure if that's really the problem here or not and if I do have to implement it, what unmanaged resources is it having a problem with that requires me to add IDisposable?
Here's the DataProvider class:DataProvider.txt
From the MSDN page about the using Statement.
Provides a convenient syntax that ensures the correct use of IDisposable objects.
and
All such types must implement the IDisposable interface.
So yes, you will need to have your DataProvider implement IDisposable to use it as you are in a using block.
DataProvider
must indeed implement IDisposable
, because it owns a IDbConnection
, which it must properly dispose of.
The "using" keyword is just a helpful way of writing exception-safe code that will call Dispose() when the variable goes out of scope. If you're not going to call Dispose(), it's not useful. That function is declared by the IDisposable interface.
If you don't have unmanaged resources, you shouldn't be using "using" on it, anyway.
A good description of this is here: http://www.codeproject.com/KB/cs/using_and_IDisposable.aspx
Yes, in order to use the 'using' statement the object needs to implement IDisposable. I looked through a bit of that code and I didn't see any unmanaged code (although I could be mistaken since there is a ton of code there).
精彩评论