Jump to content
  • Advertisement
Sign in to follow this  
Nikster

[.net] Interfaces and overloads

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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'


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;
}
}


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
Advertisement
I forgot to add, I can obviously call a none interface function from both which gets around the problem but I'm still intrigued as to what the issue is.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!