How to return variable length column of data

Mar 4, 2010 at 10:15 PM

I'm trying to return a column of numbers in a UDF. Sadly, the function signature "public static double[] getValues()" returns a row, not a column. I looked at the MoreSamples.dna file, which shows how to return a matrix of results. So I can return a column using the function "public static double[,] getValues()". BUT, being new to C#/.NET, I don't know how to construct a dynamically sized 2D array. The example code constructs a fixed sized one, which doesn't suit my needs. So I need some code that does something like:

double[,] convert(List<double> data)
{
  // please fill me in. I want to turn 'data'
  // into a 2D array with 1 column and data.Length rows.
}

I tried a function declaration of "Array getValues()", and dimensioned it dynamically via its CreateInstance static method. But ExcelDna did not recognise it as an Excel UDF.

 

Mar 4, 2010 at 10:35 PM

I've solved it! It was quite simple. I didn't know I can create a double[] array whose dimension is known only at runtime; all the examples I've seen used constants for dimensions. So the function is something like this:

double[,] convert(List<double> data)
{
  double[,] result = new double[data.Length, 1];
  for (int i = 0; i < data.Length; ++i)
    result[i,0] = data[i];
  return result;
}

Now that I can easily create a double[] array of dynamic size (but not resized dynamically), I'll simple create that instead of using a List<double>.