471,318 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

C# counterpart to VB.NET Implements

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
Feb 10 '06 #1
6 7506
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
Feb 10 '06 #2
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

"Marc Gravell" wrote:
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

Feb 10 '06 #3
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
Feb 10 '06 #4
No, VB doesn't do anything behind-the-scenes. CLR supports that.

Feb 10 '06 #5
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
Feb 10 '06 #6
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

"Niklas" wrote:
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

Feb 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jinming Xu | last post: by
reply views Thread by Thomas | last post: by
6 posts views Thread by Andrew | last post: by
reply views Thread by rodchar | last post: by
1 post views Thread by samadams_2006 | last post: by
4 posts views Thread by Han | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.