Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing 2 Interfaces with 'Same Name' Properties

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

Lets say I have 2 interfaces:

Interface ISimpleInterface
    string ErrorMsg { get; } 
End Interface

Interface IExtendedInterface
    string ErrorMsg { get; set; }    
    string SomeOtherProperty { get; set; }
End Interface

I want a class to implement both interfaces:

Public Class Foo Implements ISimpleInterface, IExtendedInterface

How do I define the ErrorMsg property in the class given that each interface has a different access level?

Here is my scenario in case you are wondering: I am writing a UserControl using a psuedo MVC arhitecture, where the UserControl exposes the extended interface to it's Controller, and exposes the Simple interface to the Consumers of the control.

By the way, implementing this in VB.NET (any suggested synatx in vb would be appreciated).

like image 271
user52212 Avatar asked Feb 10 '09 12:02

user52212


People also ask

What happens when two interfaces are created with same name?

If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously. According to JLS (§8.4. 2) methods with same signature is not allowed in this case.

Can two interfaces have same name?

But interface will contain only the declaration of the members. The implementation of interface's members will be given by the class who implements the interface implicitly or explicitly. C# allows the implementation of multiple interfaces with the same method name.

Can a class implement two interfaces with same variable names?

When two interface consists of the variable with the same name, then the class which implements those interfaces can't identify which variable to access and throws an error. Hence, to resolve that, access the variable using the interface name as the reference.

Can you implement 2 interfaces?

A class can implement more than one interface at a time. A class can extend only one class, but implement many interfaces. An interface can extend another interface, in a similar way as a class can extend another class.


2 Answers

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 { ... }} 
like image 146
thinkbeforecoding Avatar answered Sep 19 '22 07:09

thinkbeforecoding


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

like image 41
Patrick Klug Avatar answered Sep 22 '22 07:09

Patrick Klug