two interfaces having methods with same names

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have two existing interfaces having methods with same names. Now I have to
implement both intrfaces in one class. Is there any way I could implement
methods with same names in both interfaces without getting errors from the
compiler?
 
The difference between methods is determined based on 2 things, it’s name and
it’s parameters

In C# you can have multiple methods within the same class, all with the same
name, however something must separate them, such as one taking an int and
another taking a float. Take the following examples...

private void MyFunc(string value)
{
return;
}
private void MyFunc(float value)
{
return;
}
private int MyFunc(int value)
{
return value;
}
private float MyFunc(double value)
{
return (float)value;
}

All have the same name, and all take different arguments. You must be
careful when using such a method that you don’t accidentally call
MyFunc(double) when you meant to call MyFunc(float), typecasting prior to
calling can alleviate this problem. Another way is to change the order of
parameters to make such an accident nearly impossible.

Brendan
 
I forgot to mention that those methods, which have same names, also have the
same funtion signatures.

In VB6, a class can always implement interfaces with methods having same
names and method sigantures. VB6 prefixes a method name with the name of the
interface, so the potential problems could be avoided.

Since the existing interfaces I must implement in my class can not be
modified to have the method names changed, I do not really know how to find a
way to get around the problems.

Don
 
Given that these two interfaces both have a method whose name and arguments
are the same... is there no hope of having a single unified function for both?

Also in C++ you could refer to a function in a class that the current one
has derived from by prefixing the call with the class name... however in C#
the best we have is base.

In VB.NET, you can use the Implements keyword to specify which function
implements method from an interface so you can actually have:

Public Sub DoThis1(ByVal value As Integer) Implements IFoo2.DoThis

And then an IFoo2 reference and be able to call DoThis on it and have it
call the DoThis1 method of the class.

Unfortunately, C# does not give you the ability to do Implements as VB.NET
can. Worst case... you could always throw in some IL which does support it...
but that would be far worse of a nightmare.

Brendan
 
I have two existing interfaces having methods with same names. Now I have to
implement both intrfaces in one class. Is there any way I could implement
methods with same names in both interfaces without getting errors from the
compiler?

Use "Explicit Interface Implementation" :

public interface IMyIntf1
{
void DoIt();
}

public interface IMyIntf2
{
void DoIt();
}

public class MyClass : IMyIntf1, IMyIntf2
{
void IMyIntf1.DoIt()
{
...
}

void IMyIntf2.DoIt()
{
...
}
}

Joanna
 
Back
Top