interface with overloaded method

alreadyused

Regular
Joined
Jan 13, 2006
Messages
69
I am trying to learn more c# and I'm having an issue. I managed to get it to work but I want to know if there's a better way.

I have an interface in a common namespace, and a class that uses that in an application namespace.

I have overloaded a function to receive a string, integer or double, and defined that in the interface.

I'm storing it in XML and don't care about the variable type, so I want to save it as a string each time. I have to build up the xPath a bit, based on class vars, so ideally I want to call the setVal(string, string) function from the others.

But, I'm not able to say this.setVal because it claims that my class doesn't have that function.

Below is the only way I have been able to get it to work. Is there a better way?


C#:
namespace company.project.common{
	interface MyInterface{
		void setVal(string,string);
		void setVal(string,integer);
		void setVal(string,double);
        }
}

// -------------------

uses company.project.common;

namespace company.project.mainapp{
    public class MyObject : MyInterface{
        void MyInterface.setVal(string key, string value){
            string xPath; 
            // code to build xPath, verify the XmlNode exists, etc goes here but is omitted
        } // str,str

        // this works, but not sure how efficient?
        void MyInterface.setVal(string key, int value){
            MyInterface b = this;
            b.setVal(key, value.ToString());
        } // str,int

        // this does not work, but does in VB
        void MyInterface.setVal(string key, double value){
            this.setVal(key, value.ToString());
        } // str,2x
    }
}

so is that the best way of doing it? and if so, is there a term for this? sorry if that's a total noob question, I browsed around here and google and can't find anything on it.
 
Last edited by a moderator:
One thing with interfaces is a concept of implementing them Implicitly or Explicitly Implicit interfaces become part of the objects public interface and don't need any special handling - using your example the class implementing the interface could be done like.
C#:
public class MyObject : MyInterface
    {
        void setVal(string key, string value)
        {
            string xPath;
            // code to build xPath, verify the XmlNode exists, etc goes here but is omitted
        } // str,str

        // this works, but not sure how efficient?
        void setVal(string key, int value)
        {
           this.setVal(key, value.ToString());
        } // str,int

        // this does not work, but does in VB
        void setVal(string key, double value)
        {
            this.setVal(key, value.ToString());
        } // str,2x
    }
notice how the method names are not prefixed with the interface name.
 
Well that did it! Thanks for the tip! (and thanks for changing my code tags, I couldn't remember what tag to use to get the formatting)

There was one small thing that I ran into. When I dropped the interface name prefix and compiled, the error was saying that I was not implementing the method.

But I found this post:
http://www.xtremedotnettalk.com/showthread.php?t=97057&highlight=implicit+interface
that stated to use it implicitly, you must declare it public, and that solved it.

Anyway I'm putting this here in case someone else has this problem too (but it's probably just me :D )
C#:
// explicit:
void MyInterface.myMethod(string a, string b){}

// implicit but causes compile error - not implementing:
void myMethod(string a, string b){}

// implicit and compiles - stating public is required
public void myMethod(string a, string b){}

thanks again!
 
C#:
void myMethod(string a, string b){}
I believe this causes a compile error becuase the default access level is friend/internal. Since you are implementing the method from an interface, it is required to be public, thus using the public keyword resolves the error.
 
Back
Top