To implement an interface is to set up one contract between two code blocks, where the block that implements the interface is committed to implement specifically the methods defined by the interface.

At glance this sounds somehow weird. The question that arises is: Don’t I’m complicating my life? It depends…

Let’s suppose that, in the frame of a process, we have a heterogeneous set of objects that we unknown. Some have a serialization method named Save, if the method exists, then this is the responsible of the object persistence. If doesn’t exist such Save method, the object is persisted in the database by another method.

The question is how to find out, in runtime, if an object is implementing one method with a specific name? The following attempts to answer this. To develop that example, you will need to start Visual Studio and create one project of windows Console type for Visual Basic or C#.

Now we create the interface ISerializable, that contains the Save function that returns a boolean value:

[VB]

Public Interface ISerializable

Function Save() As Boolean

 End Interface

[C#]

using System;

interface ISerializable

{

bool Save();

}

By convention the names of the interfaces are prefixed with an “I” for better identification.

Then we are going to create one class, named PettyControl, with one unique property named Data of string type:

[VB]

Public Class PettyControl

Private mData As String

Public Property Data() As String

Get

Return mData

End Get

Set(ByVal value As String)

mData = value

End Set

End Property

End Class

[C#]

using System;

public class PettyControl

{

protected string Data;

public string data

{

get

{

return Data;

}

set

{

Data = value;

}

}

}

I define now one control, named myControl, that inherits from PettyControl and implements the ISerializable interface.

[VB]

Public Class myControl

Inherits PettyControl

Implements ISerializable

Public Sub New()

Data = “These are the test data…”

End Sub

Public Function Save() As Boolean

Implements ISerializable.Save

Console.WriteLine(“Saving…{0}”, Data)

Return True

End Function

End Class

[C#]

using System;

class myControl : PettyControl, IValidate, ISerializable

{

public myControl()

{

data = “These are the test data…”;

}

public bool Save()

{

Console.WriteLine(“Saving…{0}”, data);

return true;

}

}

In this way we have implemented the Save method of ISerializable interface. You will see that when typing the code line referred to the implementation of the interface, when you click on Enter, Visual Studio writes automatically the header of the function that the interface defines.

At this point, we know how to implement an Interface.

Now the problem is to know, programmatically, if one class implements some specific interface. For the example we are seeing, in concrete terms, is to know whether the class has implemented some Save method, in other words, if the class implements some serialization method.

So,we are going to write the application that will start in console, naming it App.

[VB]

Module App

Sub Main()

Console.WriteLine(“Visual Basic Version”)

Dim myControl As New myControl

Dim ser As ISerializable = DirectCast(myControl, ISerializable)

Dim success As Boolean = ser.Save

Console.WriteLine(“‘{0}’ Validation was{1}success.”, myControl.Data, IIf(success, ” “, ” not “))

End Sub

Console.ReadLine()

End Module

[C#]

class App

{

public static void Main()

{

Console.WriteLine(“C# Version”);

myControl myControl = new myControl();

ISerializable ser = (ISerializable)myControl;

bool success = ser.Save();

Console.WriteLine(“‘{0}’ Validation was{1}success.”, myControl.data, (success ? ” “ : ” not “));

}

Console.ReadLine();

}

This seems to work, but if the interface is not implemented “DirectCast(myControl, ISerializable)” in VB, or “(ISerializable)myControl” in C#, both in this case fail.

The correct way to ask is: “TypeOf (myControl) Is ISerializable”, in VB, or “myControl Is ISerializable” in C#. Let’s see:

[VB]

Module App

Sub Main()

Console.WriteLine(“Visual Basic Version”)

Dim myControl As New myControl

If TypeOf (myControl) Is ISerializable Then

Dim ser As ISerializable = DirectCast(myControl, ISerializable)

Dim anotherSuccess As Boolean = ser.Save

Console.WriteLine(“{0}’ Have{1}been successfully saved., myControl.Data, IIf(anotherSuccess, ” “, ” not “))

End If

Console.ReadLine()

End Sub

End Module

[C#]

class App

{

public static void Main()

{

Console.WriteLine(“C# Version”);

myControl myControl = new myControl();

if (myControl is ISerializable)

{

ISerializable ser = (ISerializable)myControl;

bool anotherSuccess = ser.Save();

Console.WriteLine({0}’ Have{1}been successfully saved., myControl.data, (anotherSuccess ? ” “ : ” not “));

}

Console.ReadLine();

}

}

This works fine in both languages. But there is an optimized way, in terms of MSIL.

In VB, “TryCatch” is used, which evaluates whether the interface is implemented, if doesn’t it, doesn’t return an error but instead Nothing. In C# this is analogous to the “as” operator.

[VB]

Module App

Sub Main()

Console.WriteLine(“Visual Basic Version”)

Dim myControl As New myControl

Dim serial As ISerializable = TryCast(myControl, ISerializable)

If Not serial Is Nothing Then

Dim anotherMoreSuccess As Boolean = serial.Save

Console.WriteLine(‘{0}’ Have{1}been successfully saved, with an optimized version., myControl.Data, IIf(anotherMoreSuccess, ” “, ” not “))

End If

Console.ReadLine()

End Sub

End Module

[C#]

class App

{

public static void Main()

{

Console.WriteLine(“C# Version”);

myControl myControl = new myControl();

ISerializable serial = myControl as ISerializable;

if (null != serial)

{

bool anotherMoreSuccess = serial.Save();

Console.WriteLine(‘{0}’ Have{1}been successfully saved, with an optimized version., myControl.data, (anotherMoreSuccess ? ” “ : ” not “));

}

Console.ReadLine();

}

}

The solution, with example projects may be downloaded from Interfaces.zip.

I hope this will be useful.🙂