Sign in to follow this  
Nikster

[.net] Interfaces and overloads

Recommended Posts

Hi, I've got a simple example where I'd like to call an overloaded method of a class that has an interface to avoid duplicating code but it returns the error 'error CS0120: An object reference is required for the non-static field, method, or property'

[code]
public interface IFoo
{
uint Bar(string s);
uint Bar(byte[] k);
}

public class Foo : IFoo
{
uint IFoo.Bar(string s)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return IFoo.Bar(encoding.GetBytes(s));
}
uint IFoo.Bar(byte[] b)
{
return (uint)b.Length;
}
}
[/code]

What is the correct way to call an overloaded function which is part of an interface? The error suggests to create an object, but in doing so the compiler errors saying there's no definition for Bar.

This one has me stumped.

Share this post


Link to post
Share on other sites
Ok, I was so wrapped up in how to impliment overrides of interfaces I qualified the members with IFoo which is clearly wrong in this case, if only I could delete this thread to hide my shame. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this