C# counterpart to VB.NET Implements

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

Guest

Hi
I have an Interface with some methods but I dont want the class which
implements this Interface to have the same method name as the interface
method names. In VB.NET I can use implement but have do I do it i C#?
Regards
/Niklas
 
If I understand the question correctly, implement the interface explicitely:

public interface IMyInterface {
void MyMethod(int myParameter);
}
public class MyClass : IMyInterface {
public void MyRenamedMethod(int myRenamedParameter) {
// do something
}
void IMyInterface.MyMethod(int myParameter) {
MyRenamedMethod(myParameter);
}
}

Note that you don't actually need to expose it on the *default* interface at
all...

Marc
 
I want to do something like this:

public interface IMyInterface {
void MyMethod(int myParameter);
}

public class MyClass : IMyInterface {
void MyNewMethodName(int myParameter) implements MyMethod
{
//Do something
}
}

I want the compiler to accept that I have another name for
IMyInterface.MyMethod

Regards
/Niklas
 
C# does not offer this. In fact, IIRC the framework doesn't either (due to
reflection); I suspect that VB simply does something like my code
behind-the-scenes; you could run your VB assembly through Roeder's Reflector
and see what it compiled to?

(http://www.aisto.com/roeder/dotnet/)

Marc
 
I stand corrected, then (I did say IIRC...); I have checked in reflector,
and you are correct - it is provided in the IL in a way closer to the VB
than the C#:

.method public newslot virtual final instance void RenamedTest() cil
managed
{
.override ConsoleApplication1.Interface1::Test
}

However, I think the short of it is that you still can't (unless I am wrong
again) do this in C#.

Marc
 
There is no way to replace the name, but you can offer two methods, the one
with the changed name calling the interface method.

There's a good reason you shouldn't be able to use a different name for the
interface implementing method (even though VB allows it). It's confusing to
someone using your class if the methods they expect to see, which implement
the documented interface, aren't there.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
Back
Top