Using polymorphism on an interface member

Ok, this is a bit of a Nubian question, but I ran into this today and it puzzles me somewhat WHY this would be the case.

Consider the following structure

    Public Class Employee
    Implements IPerson

    Private _MyManager As Manager
    Public Property MyManager() As Manager Implements IPerson.TheirLeader
        Get
            Return _MyManager
        End Get
        Set(ByVal value As Manager)
            _MyManager = value
        End Set
    End Property

End Class

Public Class Manager
    Implements IPerson, IAuthorityFigure

    Private _MyBoss As Boss
    Public Property MyBoss() As Boss Implements IPerson.TheirLeader
        Get
            Return _MyBoss
        End Get
        Set(ByVal value As Boss)
            _MyBoss = value
        End Set
    End Property

End Class

Public Class Boss
    Implements IPerson, IAuthorityFigure

    'Implementation code here...

End Class

Public Interface IPerson

    Property TheirLeader() As IAuthorityFigure

End Interface

Public Interface IAuthorityFigure
    'Stuff here...
End Interface

      

Here's my question: I want everyone to implement the IPerson interface, be it an employee, boss, or manager. However, they each have an attribute that relates to their leader (employees have a manager, managers have a boss, etc.). Each implementation of their leader can potentially be different from others, apart from the fact that they implement the IAuthorityFigure interface. I want the IPerson interface to have an IAuthorityFigure property, but a compiler error occurs for me when I implement this b / c, IAuthorityFigure is not the same type as Boss or Manager (although they do implement the interface).

So, I am doing something wrong, or this is a limitation of using interfaces or abstract classes. If this is a limitation, can anyone explain why this is so?

Thanks!

0


a source to share


2 answers


You are not doing anything wrong. In all currently released versions of Visual Basic (and C #), the implementation of a method, property, or field of an interface must match the signature on the interface exactly. There is no way to override this.

The best you can do is have 2 properties, one that implies an interface and the other that is used to return the actual implementation.



Private _MyBoss As Boss
Public Property TheirLeader() As IAuthorityFigure Implements IPerson.TheirLeader
    Get
            Return MyBoss
    End Get
    Set(ByVal value As IAuthorityFigure)
            MyBoss = CType(value, IAuthorityFigure)
    End Set
End Property

Public Property MyBoss As Boos
    Get
            Return _MyBoss
    End Get
    Set(ByVal value As Boss)
            _MyBoss = value
    End Set
End Property

      

+1


a source


This will help if you have provided some code that defines the IPerson interface. However, I would suggest that you need to cast Manager and Boss as IAuthorityFigure instead of your classes.



0


a source







All Articles