PDA

Click to See Complete Forum and Search --> : inheriting IDisposable...for using {} blocks.


effekt26
Jul 1st, 2007, 07:11 PM
Greetings,

Say i have the following class, which read has a function to return a dataset full of config info...:

class ReadConfig {

//logic to read config goes here, and config uses a dataset...

}

what i want is to able to use the using {} so that it can dispose. I know this is done like so...

class ReadConfig : IDisposable {

void IDisposable.Dispose() { }

//logic to read config here...

}

ok, so my question is, if im using a using{} block in my code to handle this, will is automatically dispose that dataset that it returns, as its not referenced, or do i manually need to call the dispose() method from within the function to return the dataset, or do i need to add an overloaded method, and dispose it in there??

Cheers, Justin

jmcilhinney
Jul 2nd, 2007, 01:27 AM
Firstly, IDisposable is an interface, not a class, so you implement it, not inherit it.

Secondly, have you read the MSDN documentation for the IDisposable interface? That should have been the first thing you did.

The point of implementing the IDisposable interface is to implement a Dispose method. That method does nothing by default, just like any other method. If your class holds unmanaged resources itself, or else holds managed resources that themselves support disposal, it is in the Dispose method that you are supposed to release them. The MSDN documentation for the IDisposable interface shows you exactly how to do just that. A member variable of type DataSet would qualify as managed resources.

effekt26
Jul 2nd, 2007, 01:55 AM
Firstly, IDisposable is an interface, not a class, so you implement it, not inherit it.

Secondly, have you read the MSDN documentation for the IDisposable interface? That should have been the first thing you did.

The point of implementing the IDisposable interface is to implement a Dispose method. That method does nothing by default, just like any other method. If your class holds unmanaged resources itself, or else holds managed resources that themselves support disposal, it is in the Dispose method that you are supposed to release them. The MSDN documentation for the IDisposable interface shows you exactly how to do just that. A member variable of type DataSet would qualify as managed resources.

Hi,

Thanks for the clearing up the implement, not inherit...i'm still trying to get my head around it... :eek:

Also, i did read the msdn page for IDisposable, but was unable to fully understand it, as my DataSet is not a member variable, as it was only used in one function.

but it makes sense, that if i want to dispose it, it must be a member variable that implements IDisposable.

So just to clarify, I should make the dataset a member variable, add the Dispose(true) and GC.SuppressFinalize(this).

As well as Dispose(bool disposing) and the destructor...

i still dont know how it understands that it needs to Dispose the Dataset, when im disposing my class.

Or, because it implements the IDisposable, will it call the Dispose() method for any member variables that implements IDisposable...??

Thanks,
Justin

jmcilhinney
Jul 2nd, 2007, 02:01 AM
If your class doesn't hold any unmanaged resources or have any member variables whose type has a Dispose method then your class doesn't need to implement the IDisposable interface. If your class creates a DataSet in a method and only uses it in that method then the DataSet should be disposed at the end of that method. That's why you don't understand how the class would know to dispose the DataSet: because it wouldn't.

effekt26
Jul 9th, 2007, 12:13 AM
Ok,

So I am trying to implement IDisposable for a Lotus Class, which I know does NOT contain a dispose method.

i have the following code...



try {

using (LotusStuff _lotus = new LotusStuff("password")) {

//do stuff

}

SqlConnection _conn = new SqlConnection("kadsklasdjaklsdalksdj");
_conn.Open();

} catch (Exception ex) {

throw new Exception(ex.Message);

}

//and then my lotus class...

class LotusStuff : IDisposable {

void IDisposable.Dispose() {
GC.SuppressFinalize(this);
}

public LotusStuff() {}
public LotusStuff(string Password) {

NotesSession _ntsSession = new NotesSession();
_ntsSession.Initialize(Password);

}

}

now, when I comment out the using {} block, the try {} catch {} works as it should. and throws the exception.

However, when the Lotus Objects are being used (uncomment using{}) I get the DeBugger popup come up...because the lotus object is now handling my error, and ignores my try catch blocks...

so what I am hoping to do is fully release the NotesSession by Implementing IDisposable, but it just doesnt seem to be releasing it...?

Cheers,
Justin

jmcilhinney
Jul 9th, 2007, 12:34 AM
I have no idea what you're trying to accomplish. What is it that you think you need to release by disposing an instance of this type?

effekt26
Jul 9th, 2007, 12:41 AM
Well, Actually, im not really sure myself. But it seems that when my application has to use the Lotus Domino Objects (Interop.Domino.Dll), it like, takes control of my code or runs in a seperate thread, because, if i get a runtime error, regarding notes or not, i opens to Lotus Error Notification, called nsd.exe, and then kills the process that started it...being my application, rendering my try catch blocks useless.

But, if my code does not have use any of those domino objects, the try catch works fine. so what I am hoping to achieve is creating a dispose method, so that it will complety release the domino objects when they have finished being used, and then give control back to my try catch blocks....

sorry if that doesnt make too much sense, but I dont really understand why it is happening myself, and is very frustrating...

Cheers,
Justin