C Sharp

Implementing Interfaces

Because an interface defines a contract, any class that implements an interface must define each and every item in that interface or the code won't compile. Using the IValidate example from the previous section, a client class would need to implement only the interface's methods. In the following example, I have a base class called FancyControl and an interface called IValidate. I then have a class, MyControl, that derives from FancyControl and implements IValidate. Note the syntax and how the MyControl object can be cast to the IValidate interface to reference its members.

using System;
public class FancyControl
{
    protected string Data;
        public string data
        {
            get
        {
            return this.Data;
        }
        set
        {
            this.Data = value;
        }
    }
}
interface IValidate
{
    bool Validate();
}
class MyControl : FancyControl, IValidate
{
    public MyControl()
    {
        data = "my grid data";
    }
    public bool Validate()
    {
        Console.WriteLine("Validating...{0}", data);
                return true;
    }
}
class InterfaceApp
{
    public static void Main()
    {
        MyControl myControl = new MyControl();
        // Call a function to place the control on
        // the form. Now, when the editor needs to
        // validate the control, it can do
        // the following:
        IValidate val = (IValidate)myControl;
        bool success = val.Validate();
    Console.WriteLine("The validation of '{0}' was {1}successful",
                      myControl.data,
                      (true == success ? "" : "not "));
    }
}

Using the preceding class and interface definition, the editor can query the control as to whether the control implements the IValidate interface. (You'll see how to accomplish this step in the next section.) If it does, the editor can validate and then call the implemented interface methods. You might be prompted to ask, "Why don't I just define a base class to use with this editor that has a pure virtual function called Validate? The editor would then accept only controls that are derived from this base class, right?" -

That would be a workable solution, but it would be severely limiting. Let's say you create your own controls and they all derive from this hypothetical base class. Consequently, they all implement this Validate virtual method. This works until the day you find a really cool control that you want to use with the editor. Let's say you find a grid that was written by someone else. As such, it won't be derived from the editor's mandatory control base class. In C++, the answer is to use multiple inheritance and derive your grid from both the third-party grid and the editor's base class. However, C# does not support multiple inheritance.

Using interfaces, you can implement multiple behavioral characteristics in a single class. In C# you can derive from a single class and, in addition to that inherited functionality, implement as many interfaces as the class needs. For example, if you wanted the editor application to validate the control's contents, bind the control to a database, and serialize the contents of the control to disk, you would declare your class as follows: -

public class MyGrid : ThirdPartyGrid, IValidate,
                      ISerializable, IDataBound
{
image
}

As promised, the next section answers the question, "How can one piece of code know when a class implements a given interface?" -