Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Implementing Existing Interfaces

Visual Studio .NET 2003

An interface is a contract. It defines a standard set of properties, methods, and events that are to be found in any class that implements the interface. This insures that if your class for example implements IWidget, any other class that expects to interact with IWidget can interact with your class, regardless of what other functions your class may perform.

Interfaces and their member functions and properties are implemented in Visual Basic using the Implements keyword. In C#, interfaces to be implemented follow a colon (:) in the class declaration and are separated on a single line by commas.

The property and method definitions must take the exact form defined by the interface. For example, say you were implementing the following interface:

' Visual Basic
Public Interface IAccount
   Sub DeductFees(Fees as IFeeSchedule)
   Sub PostInterest()
   Property Balance() As Decimal
End Interface

// C#
public interface IAccount
   void DeductFees(IFeeSchedule feeSchedule);
   void PostInterest();
   decimal Balance 

In the code that you would write to implement these properties and methods, you would have to follow the construction of these procedures exactly. For instance, the following line would be illegal:

' Visual Basic
Public Property MyBalance() As Double Implements IAccount.Balance

// C# 
double IAccount.MyBalance

This line is not a legal implementation of the interface because it declares the property as a double, instead of an integer. Likewise, a method must take the same number and type of arguments as are defined in the interface. It also has to have the same name.

To implement an existing interface

  1. Declare a class using the standard syntax.
  2. Within the class declaration, define the interface you are implementing. A single class may implement more than one interface.

    The following examples show how to specify that the IAccount, ICustomer, and IPortfolio interfaces be implemented:

    ' Visual Basic
    Public Class BusinessAccount
       Implements IAccount
       Implements ICustomer
       Implements IPortfolio
       ' Code to implement class here.
    End Class
    // C#
    public class BusinessAccount : IAccount, ICustomer, IPortfolio
       // Code to implement class here.
  3. Implement each property and method required by the interface. Examples are shown below:
    ' Visual Basic
    Public Sub BuyEquity() Implements IPortfolio.PurchaseEquity
       ' Code to carry out ths procedure goes here.
    End Sub
    Public Property Balance() as Decimal Implements IAccount.Balance
       ' Appropriate code goes here.
    End Property
    Public Function ChangeAddress(NewAddress as IAddress) as Boolean _
       Implements ICustomer.ChangeAddress
       ' Appropriate code is here.
    End Function
    // C#
    bool ICustomer.ChangeAddress(IAddress newAddress)
       // Appropriate code goes here.
    void IPortfolio.BuyEquity()
       // Code to carry out this procedure goes here.
    public decimal Balance 
       // Appropriate code goes here.
    Note   In Visual C#, you cannot implement an interface member with a method of a different name, such as you can in Visual Basic .NET using the Implements keyword.
  4. Write any other code you require your class to contain.

See Also

Interface Definition and Factoring Design Considerations | Design Recommendations for Interfaces and Components | Recommendations for Abstract Classes vs. Interfaces

© 2015 Microsoft