[Last Call] Learn how to a build a cloud-first strategyRegister Now


How does one implement an interface??

Posted on 2007-08-11
Medium Priority
Last Modified: 2010-04-15
I am not sure how to implement an interface...

I would like to implementSystem.IDisposable  on the following method in order to use the using statements to call Dispose() implicitly. This code runs fine until I reference System.IDisposable interface at the end of the method declaration. I don't think the interface is being accessed so I explicitly called Close() and Dispose() in the finally block.

What is the correct way to implement IDisposable???


private void ScaleWtsDataSetForm_Load(object sender, System.EventArgs e):System.IDisposable
   OleDbConnection Conn = new OleDbConnection();
   OleDbDataAdapter da = new OleDbDataAdapter();
   OleDbCommandBuilder oleDbCb = new OleDbCommandBuilder();
   //declared upstairs in declarations so don't need to declare again here
   //oleDbCommand1 = new OleDbCommand(SQL, Conn);      
      //Close() and Dispose called implicitly when using the "using" blocks
      using(Conn = new OleDbConnection(ConStr))
         using(oleDbCommand1 = new OleDbCommand(SQL, Conn))
         using(da = new OleDbDataAdapter())
            da.SelectCommand = oleDbCommand1;
            using(oleDbCb = new OleDbCommandBuilder(da))
            {}//end using(oleDbCb = new OleDbCommandBuilder(da))
            da.Fill(dataSet1, "Stats");
            dataGrid1.SetDataBinding(dataSet1, "Stats");
         }//end using(da = new OleDbDataAdapter())
                     }//end using (oleDbCommand1 = new OleDbCommand(SQL, Conn))
                  }//end using(Conn = new OleDbConnection(ConStr))
               }//end try
           }//end ScaleWtsDataSetForm_Load(object sender, System.EventArgs e)
Question by:Saxitalis
LVL 22

Expert Comment

ID: 19678075
An interface defines a set of methods and/or properties. Any class that implements that interface MUST contain any methods and properties defined in the interface.
LVL 27

Accepted Solution

Dabas earned 2000 total points
ID: 19678138
Hello Saxitalis,

If you use VS2005 then the IDE helps you add all of the methods and properties.
Assuming you class is called MyClass, when you type

class MyClass:IDisposabl, a little line appears under the letter 'I' of IDisposable. Press Ctrl-. (Ctrl-FullStop) and a little context menu will say something like  "Implement Inteface IDispoable"
Clicking on that menu item will have the IDE add all the methods and properties as stubs for you.

You will see that all it does in the IDisposable case is to add a void IDisposable.Dispose() method. Within that method you should dispose of any UNMANAGED objects you might have created.



Author Comment

ID: 19678986
Hi Dabas - thank you very much!!  Sax

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question