Functions with the same signatures

Nov 24, 2011 at 6:44 PM

Hi,

Happy Thanksgiving!!!

How can I make ExcelDNA populate tow UDF functions with the same signature, but in different class?

Best Regards,

GW

Coordinator
Nov 24, 2011 at 7:09 PM
Edited Nov 24, 2011 at 7:15 PM

Hi GW,

I think the 'last' function that Excel-DNA finds via reflection will be the one that 'wins' and is called from Excel.

Say you have:

 

public class FirstClass
{
   public static object MyFunction(string param1)
   { ...}
}


public class SecondClass
{
   public static object MyFunction(double param1)
   { ...}
}

 

Then you could rename the Excel function names that are registered using the ExcelFunction attribute:

 

public class FirstClass
{
   [ExcelFunction(Name="MyFunction.FromFirst")]
   public static object MyFunction(string param1)
   { ...}
}


public class SecondClass
{
   [ExcelFunction(Name="AnotherNameHere")]
   public static object MyFunction(double param1)
   { ...}
}

 

Or perhaps make a single function that switches between the overloaded functions:

 

public class ExportedFunctions
{ 
   [ExcelFunction]
   public static object MyFunction(object param)
   {
      if (param is double) 
         return FirstClass.MyFunction((double)param);
       if (param is string)
         return SecondClass.MyFunction((string)param);

      return ExcelError.ExcelErrorValue;
   }
}

 

To ensure that only the ExcelFunction-decorated functions are registered with Excel, you add an attribute in your .dna file. (Else you might find the wrong 'MyFunction' is registered last and is the one called from Excel.)

 

<DnaLibrary Name="My AddIn" RuntimVersion="v4.0" >
   <ExternalLibrary Path="MyFunctions.dll" ExplicitExports="true" />
</DnaLibrary>

 

 

Might this work for you?

-Govert

Nov 24, 2011 at 8:44 PM

Govert,

You are my Thanksgiving hero!

Exactly it is the Turkey I want today!

Have a nice holiday!

GW