C# Using a bidimensional array dinamically RRS feed

  • Question

  • I’m developing a C# program that has a function that executes a “Select” command in a table and returns a number of registries. I pass to the function a list with the columns that I need and an array to receive the data. This array has 2 dimensions: 1 for the columns and the other for the results. My problem is that I don’t know how many registries will be retrieved and, therefore I can’t establish the size of the array. Does anyone have a suggestion on how I can do this? Thanks.

    Friday, March 20, 2020 7:37 PM

All replies

  • In this case, you would use a List<int[]> instead of an int[,] . That is, a list of unidimensional arrays instead of a bidimensional array.

    The routine that reads the registers can use the .Add method of the List to add each row dynamically. And once it is finished, if you wish you can move the data from the list into an array (it's easy because now you know the number of rows).

    To do this, you would not pass to the method the array that receives the data. Instead, the method creates the array and returns it to the caller. Or you can pass it by reference and let the method initialise the reference. You cannot pass it already allocated because you don't know the number of rows before passing it, so it has to be allocated inside the method.

    Friday, March 20, 2020 8:26 PM
  • Hi,

    My understanding of your question is that you can use this way to getting data into a 2D array.

    class Program
            static void Main(string[] args)
                object[,] My2DArry = new Object[PersonTable().Rows.Count + 1, PersonTable().Columns.Count];
                for (int j = PersonTable().Rows.Count - 1; j >= 0; j--)
                    // get row items
                    var rowItem = PersonTable().Rows[j].ItemArray;
                    // add to resulting My2DArry array
                    for (int k = 0; k <= rowItem.Length - 1; k++)
                        My2DArry[j + 1, k] = rowItem[k];
                // Display Column Names
                foreach (DataColumn column in PersonTable().Columns)
                    Console.Write(column.ColumnName + "\t");
                //Display Result
                for (int i = 0; i < My2DArry.GetLength(0); i++)
                        for (int j = 0; j < My2DArry.GetLength(1); j++)
                            Console.Write(My2DArry[i, j] + "\t");
            static DataTable PersonTable()
                DataTable Mytable = new DataTable();
                Mytable.Columns.Add("ID", typeof(int));
                Mytable.Columns.Add("Name", typeof(string));
                Mytable.Columns.Add("Age", typeof(int));
                Mytable.Rows.Add(1, "David", 19);
                Mytable.Rows.Add(2, "Sam", 15);
                Mytable.Rows.Add(3, "Daniel", 20);
                return Mytable;
    The output
    ID      Name    Age
    1       David   19
    2       Sam     15
    3       Daniel  20

    Please remember to mark the replies as answers if they helped you :) ~

    Friday, March 20, 2020 8:39 PM