Get a value from int array by index

Let's say I have an input for the report which has a simple integer array.
I knew the array has a fixed size of 3.

public class Input {
  public int[]  OneTwoTree {get;set;}

  public Input(){ OneTwoTree = new int[] {1,2,3};

report.AddTable("Inputs", new List<Input> {new Input()});

Is it possible to get the array's value by index with or without creating a name range for OneTwoTree?


While there is no built in support for this, you could do this with an used defined function:

    public class TArrayIndexImp : TFlexCelUserFunction
        public override object Evaluate(object[] parameters)
            if (parameters == null || parameters.Length != 2)
                throw new ArgumentException("Bad parameter count in call to IDX(Data, Index) user-defined function");

            if (!(parameters[0] is int[])) throw new ArgumentException("The first parameter of IDX must be an array of integers.");
            int[] Data = (int[])parameters[0];
            if (!(parameters[1] is double)) throw new ArgumentException("The second parameter of IDX must be a number.");

            int idx = (int)(double)parameters[1];

            if (idx < 0) throw new ArgumentException("The index in IDX must be >= 0.");
            if (idx >= Data.Length) throw new ArgumentException("The index in IDX must be less than the length of the data.");

            return Data[idx];

                report.AddTable("Inputs", new List<Input> {new Input()});
                report.SetUserFunction("IDX", new TArrayIndexImp());

And in the template write:

This would return the third item of the array.


Hi Adrian,

Thanks for the answer, but it seems like this user function doesn't work for me.

The parameters[0] is not an array, it is "FlexCel.Core.TRichString" object and as a result I got the "The first parameter of IDX must be an array of integers." exception.

p.s. I'm using FlexCel.dll

I'm sorry, my fault. I've just missed some tags.