How does one implement an interface??

Posted on 2007-08-11
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

    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

    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

    Hi Dabas - thank you very much!!  Sax

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now