Wednesday 25 August 2010

Multiple inheritance with interfaces in VB.Net

I couldn’t recall the correct Visual Basic .Net syntax for creating an interface which itself extends other interfaces. So, as a reminder, here it is.

Lets say we have 2 simple interfaces: INameable and IIdentifiable. These interfaces simply require that implementing classes have an Id property and a Name property respectively. Lets also say that we want to define an IReferenceData interface that simply says implementing classes must implement INameable and IIdentifiable.

For comparison lets start with C#:

namespace Domain
{
    /// <summary>
    /// Classes implementing this interface will have a name property.
    /// </summary>
    public interface INameable
    {
        /// <summary>
        /// Gets or sets the name of the entity.
        /// </summary>
        string Name { get; set; }
    }

    /// <summary>
    /// Classes implementing this interface will have an Id property.
    /// </summary>
    public interface IIdentifiable
    {
        /// <summary>
        /// Gets or sets the unique ID of the entity.
        /// </summary>
        int Id { get; set; }
    }

    /// <summary>
    /// An interface to be implemented by reference data types (simple types with 
    /// Name and ID properties).
    /// </summary>
    public interface IReferenceData : IIdentifiable, INameable
    {
    }
}

Now, the same thing in VB.Net:

Namespace Domain
    ''' <summary>
    ''' Classes implementing this interface will have a name property.
    ''' </summary>
    Public Interface INameable
        Property Name As String
    End Interface

    ''' <summary>
    ''' Classes implementing this interface will have an Id property.
    ''' </summary>
    Public Interface IIdentifiable
        ''' <summary>
        ''' Gets or sets the unique ID of the entity.
        ''' </summary>
        Property Id As Integer
    End Interface

    ''' <summary>
    ''' An interface to be implemented by reference data types (simple types with 
    ''' Name and ID properties).
    ''' </summary>
    Public Interface IReferenceData
        Inherits IIdentifiable
        Inherits INameable
    End Interface
End Namespace

Note the separate Inherits statements on separate lines.