none
can I overload a method in a class? RRS feed

  • Question

  • I have a public class with two helloWorld methods -- one method is an int, the other is a string:

    ...
    public class Class1:IMyClass1
        {
            public Class1()
            {
                //constructor.. create object..
            }

            public int helloWorld()
            {
                return 10;
            }

            public string helloWorld(string s1)
            {
                return s1;
            }
    ...
    )

    when I instantiate this class I can debug.print the first helloWorld (the one with no argument), but if I add the 2nd helloWord with and arg   like Debug.Print x.helloWorld("test")  I get a compile error -- wrong number of args.   Can I overload my helloWorld method?  How to pass an arg to it?

    Thanks

     


    Rich P
    Tuesday, August 2, 2011 9:58 PM

Answers

  • Actually, this is a com visible library.  I am using it in Excel VBA.  The library is in C#.  I figured out the problem:  I am using an interface which did not include a second helloWord method.  When I added the overloaded helloWorld method to the interface and then to the class -- it appeared in the dropdown (intellisense) in excel as helloWorld_2, and I was able to add a string arg and debug.print everything OK.
    Rich P
    Tuesday, August 2, 2011 10:38 PM

All replies

  • I think you have a syntax error; missing parentheses.

    Debug.Print( x.helloWorld("test"));
    

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Tuesday, August 2, 2011 10:20 PM
    Moderator
  • Actually, this is a com visible library.  I am using it in Excel VBA.  The library is in C#.  I figured out the problem:  I am using an interface which did not include a second helloWord method.  When I added the overloaded helloWorld method to the interface and then to the class -- it appeared in the dropdown (intellisense) in excel as helloWorld_2, and I was able to add a string arg and debug.print everything OK.
    Rich P
    Tuesday, August 2, 2011 10:38 PM