Results 1 to 7 of 7

Thread: inheriting IDisposable...for using {} blocks.

  1. #1

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    inheriting IDisposable...for using {} blocks.

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: inheriting IDisposable...for using {} blocks.

    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.
    Last edited by jmcilhinney; Jul 2nd, 2007 at 01:31 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Re: inheriting IDisposable...for using {} blocks.

    Quote Originally Posted by jmcilhinney
    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...

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: inheriting IDisposable...for using {} blocks.

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Re: inheriting IDisposable...for using {} blocks.

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

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: inheriting IDisposable...for using {} blocks.

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Re: inheriting IDisposable...for using {} blocks.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width