Serving the Quantitative Finance Community

 
User avatar
rockinsquat
Topic Author
Posts: 1
Joined: March 20th, 2009, 6:51 pm

UDF (User Defined Function) for excel in c#

February 25th, 2012, 5:23 pm

Hi all,He're what I've done :I've created a class library project inc #, with the following code : You can see a function doing a multiplication (MultiplyNTimes), an other (CalculateArea) taking a range and calculating it's "area", and a last one (MatrixMultiplication) taking to ranges which shoul symabolize matrices, and trying to output a range containing the product of the two matrices, but I cant succeed in converting myArray in whatever outputing something in excel when I use the MatrixMultiplication function...In fact, i coul succeed in outputting a "static" range, by for instance putting object[,] myArray = {{2,3}{3,8}} and a return myArray, but the range to be outputted should depend on the size of ranges range1 and range2, so the object[,] should be dynamic...(When I compile the code, in excel I add an automation reference to AutomationAddin.MyFunctions so that I could use my UDF's in Excel.)Thx
Last edited by rockinsquat on February 24th, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
zhouxing
Posts: 1
Joined: March 24th, 2006, 4:17 pm

UDF (User Defined Function) for excel in c#

February 27th, 2012, 2:12 am

Not fully understand your question, but if you are asking how to write a self-adapting array function, there is an example here: http://book.greenwich2greenwich.com/Examples/BTW, I have also noticed something in that code that may be worthy discussion:1- it's better to always mark a UDF return type as "object" ... it does not cause any side-effect if the code completes normally, but allows you to put a try..catch and to return Exception.ToString() if necessary.2 - use (double)(range.value) instead of Convert.ToDouble() ... the former is easier and usually faster
 
User avatar
rockinsquat
Topic Author
Posts: 1
Joined: March 20th, 2009, 6:51 pm

UDF (User Defined Function) for excel in c#

February 27th, 2012, 1:21 pm

Thx a lot zhouxing ! You've perfectly well understood what I wanted ! (I was insisting on object[,] rather than object because I found some thread on stackoverflow advising this in the framework I'm under, that is, creating UDFs with cs.) Btw thank you for your others advises, as well as for the link, that will helped code all this stuff in a more human manner, with catching exceptions aso... )
Last edited by rockinsquat on February 26th, 2012, 11:00 pm, edited 1 time in total.