Com Interop with Function Overloading in c# RRS feed

  • Question

  • Is there a way to define which overloaded function should be the default when registering with COM?

    Here is an example of what I have

    public class MyClass
      public int load(int MyInt)
      public int load()

    When I register this class I get two functions



    load = load([in] long MyInt);
    load_2 = load_2();

    Is there a way to declare which function will recieve the default method name? 
    I need the above to look like this when exposed:
    load = load();
    load_2 = load_2([in] long MyInt);

    Thanks for any help!

    Wednesday, April 22, 2009 6:47 PM

All replies

  • Extract an interface from your class, give unique names to overloaded  methods, and make it COM visible.
    Derive your class from the interface. Use the interface to access your class.

    MSMVP VC++
    Wednesday, April 22, 2009 8:06 PM
  • COM doesn't support method overloads.  Using different function names is a trivial solution.
    Hans Passant.
    Wednesday, April 22, 2009 8:43 PM
  • Maybe I didn't explain this well enough.

    I have classic asp making calls to this dll.  From Classic asp there are several hundred calls to the load() method but only a few to the load(int) method.

    When this dll is registered I want the load() method to keep the same name and not be automatically renamed to load_2().

    Changing the function names inside the class is also an undesirable path as both functions are called from the same class many different times and this would require me to change all of those calls.

    I accept that I will need to make changes to the asp code, I would just prefer to make the changes to the load(int) calls as there are only a few of those.

    Please let me know if you need any other information.

    Wednesday, April 22, 2009 9:08 PM
  • In this case, decorate the parameters with the Optional attribute. Optional parameters are supported by COM.
    MSMVP VC++
    Wednesday, April 22, 2009 9:14 PM
  • Reverse the order of the overloads in the source code file, ensure load() is first.
    Hans Passant.
    Thursday, April 23, 2009 1:49 AM
  • Sheng, I don't believe c# supports optional parameters.  Please correct me if I am wrong.

    nobugz. I tried that and it didn't seem to work.  I assume this is to preserve compatability between builds.  Is there somewhere these settings are stored that I can deleted them?
    Thursday, April 23, 2009 1:49 PM
  • It is not optional in C# language but optional in COM.
    MSMVP VC++
    Thursday, April 23, 2009 1:53 PM
  • Sheng,  Thanks for the link, I think this might be what I'm looking for but I can't seem to make it work correctly.  Would you be kind enough to provide me with an example of how this would work?
    Thursday, April 23, 2009 2:56 PM
  • public int load([in, optional] ref object MyInt) { ... }

    MSMVP VC++
    Thursday, April 23, 2009 5:28 PM
  • Now I'm getting this error:

    Error 65 Overload resolution failed because no accessible 'load' is most specific for these arguments:
        'Public Overrides Function load([MyInt As Integer]) As Integer: Not most specific.
        'Public Overridable Function load() As Integer: Not most specific.

    Thursday, April 23, 2009 8:34 PM
  • Merge your two load functions into one. You need decide whether to call the load block currently in Load() or the code block currently in Load(int) based on the type of the optional parameter.

    MSMVP VC++
    Thursday, April 23, 2009 8:42 PM
  • Thnaks, I should have figured that one out on my own.  I'm not at my workstation right now to try, but will this cause problems for calls to this function from other classes in the same dll?

    Thursday, April 23, 2009 8:54 PM