Ask a questionAsk a question
 

AnswerAlternative for Optional arguments or method overload?

  • Friday, April 27, 2007 7:50 PMgalexyu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

     

    I saw in one of the comments on this blog: http://blogs.msdn.com/cumgranosalis/archive/2006/04/11/WebServiceAsyncUdfs.aspx#1282044 that UDFs do not support overloading of methods.

    I also read in this article: http://msdn2.microsoft.com/en-us/library/ms495224.aspx that a UDF method cannot have Optional arguments.

     

    I am trying to create UDF methods with same name, but with different number of parameters for Excel Services. Something like:

     

     

            [UdfMethod(IsVolatile = true)]

            [ComVisible(false)]

            public object MyUDFMethod(object[,] arg1)

    {

    ......

    }

     

            [UdfMethod(IsVolatile = true)]

            [ComVisible(false)]

            public object MyUDFMethod(object[,] arg1, object[,] arg2)

    {

    ......

    }

     

    OR

     

            [UdfMethod(IsVolatile = true)]

            [ComVisible(false)]

            public object MyUDFMethod(object[,] arg1, [Optional]object[,] arg2)

    {

    ......

    }

     

     

    Is there any alternative to have this supported by Excel Services?

     

    Thanks.

     

    Aleksandra

     

     

Answers

  • Tuesday, May 01, 2007 4:28 PMdannykhen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You could use "param arrays" - a trailing set of alternating # of arguments, that your UDF gets as an array.

     

            [UdfMethod(IsVolatile = true)]

            public object MyUDFMethod(params object[] arg)

    {

    ......

    }

     

    Each element in a param array can also be an array in itself, so if you need each one to be an object[,], you could define the param array as "params object[][,]".

    The only thing I am not sure of is how this works as a COM interface.

     

    Danny

All Replies

  • Tuesday, May 01, 2007 4:28 PMdannykhen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You could use "param arrays" - a trailing set of alternating # of arguments, that your UDF gets as an array.

     

            [UdfMethod(IsVolatile = true)]

            public object MyUDFMethod(params object[] arg)

    {

    ......

    }

     

    Each element in a param array can also be an array in itself, so if you need each one to be an object[,], you could define the param array as "params object[][,]".

    The only thing I am not sure of is how this works as a COM interface.

     

    Danny

  • Tuesday, May 01, 2007 8:28 PMgalexyu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Actually, first suggestion is not working for some reason:

     public object MyUDFMethod(params object[] arg)

     

    But, the second one solves the problem:

     public object MyUDFMethod(params object[][,] arg)

     

    Thanks a lot.

     

    Aleksandra