Implementing 2 Interfaces with 'Same Name' Properties

后端 未结 8 1782
天涯浪人
天涯浪人 2021-01-04 22:50

This seems like a reasonable (and maybe simple?) scenario, but how would you do the following:

Lets say I have 2 interfaces:

Interface ISimpleInterfa         


        
相关标签:
8条回答
  • 2021-01-04 23:03

    While explicit implementation will solve this, as shown by others, in this very case i would probably let IExtendedInterface implement ISimpleInterface (ErrorMsg property is semantically the same property, it means the same in these 2 interfaces i would guess).

    interface ISimpleInterface
    {
        string ErrorMessage { get; set; }
    }
    interface IExtendedInterface : ISimpleInterface
    {
        string SomeOtherProperty { get; set; }
    }
    
    0 讨论(0)
  • 2021-01-04 23:16

    In C# you can use explicit interface implementation:

    class Foo
    {
        string ISimpleInterface.ErrorMsg
        { get... }
    
        string IExtendedInterface.ErrorMsg
        { get... set... }
    
        string IExtendedInterface.SomeOtherProperty
        { get... set... }
    }
    

    or Interface Mapping

    class Foo
    {
        public string ErrorMsg
        { get... set... }       
    
        public string SomeOtherProperty
        { get... set... }
    }
    

    As for VB.NET, it has Implements keyword:

    Property ErrorMsg As String Implements ISimpleInterface.ErrorMsg
    
    Property OtherErrorMsg As String Implements IExtendedInterface.ErrorMsg
    
    0 讨论(0)
  • 2021-01-04 23:21

    The Implements keyword in VB.NET makes this easy:

    Public Interface ISimpleInterface
      ReadOnly Property ErrorMsg() As String
    End Interface
    
    Friend Interface IExtendedInterface
      Property ErrorMsg() As String
      Property SomeOtherProperty() As String
    End Interface
    
    Public Class Foo
      Implements ISimpleInterface, IExtendedInterface
      Private other As String
      Private msg As String
    
      Public Property ErrorMsgEx() As String Implements IExtendedInterface.ErrorMsg
        Get
          Return msg
        End Get
        Set(ByVal value As String)
          msg = value
        End Set
      End Property
    
      Public Property SomeOtherPropertyEx() As String Implements IExtendedInterface.SomeOtherProperty
        Get
          Return other
        End Get
        Set(ByVal value As String)
          other = value
        End Set
      End Property
    
      Public ReadOnly Property ErrorMsg() As String Implements ISimpleInterface.ErrorMsg
        Get
          Return msg
        End Get
      End Property
    End Class
    
    0 讨论(0)
  • You can implement one of them or both interfaces with an 'explicit interface' implementation, so the compiler knows which ErrorMsg property belongs to which interface.

    To do this simply write :ISimpleInterface (for C#) after your class name and then click on ISimpleInterface and select implement explicit interface.

    More information here: http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx

    0 讨论(0)
  • 2021-01-04 23:22

    You will need to work with Explicit Interface Implementations. More on the subject here: http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx

    0 讨论(0)
  • 2021-01-04 23:23

    Sorry but I don't master VB.Net syntax.

    In C# you can implement interfaces implicitly or explicitly. If your class Foo implements ErrorMsg as a public method, this implementation will be used for both interface.

    If you want distinct implementation you can implement it explicitly :

    string ISimpleInterface.ErrorMsg {get { ... } } 
    string IExtendedInterface.ErrorMsg {get { ... } set { ... }} 
    
    0 讨论(0)
提交回复
热议问题