Convert a List<object> to a string[,] 2D array

Socarsky

Well-known member
Joined
Mar 3, 2014
Messages
59
Programming Experience
Beginner
I am in a difficulty to convert a List that is class object which populated from database, so far so good to have that list of object but I need its string[,] (2D) array as converted to apply a method which helps to get an print output.

C#:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    lCT = CTKayitlarDonsunBakalim(dateTimePicker1.Value);
    // ==> here i need a method which copies/clones List<object>
}


private List<Maintable.QueryCT_Qty> CTKayitlarDonsunBakalim(DateTime d)
{
    List<Maintable.QueryCT_Qty> lSwap = new List<Maintable.QueryCT_Qty>();
    DateTime PickedDateTime = d.Date;
    DataAccess da = new DataAccess();
    lSwap = da.CheckCTRecords(PickedDateTime);
    return lSwap;
}
 
There is no magic C# syntax. You need to create a 2 dimensional string array and then assign values to each element.

Doing two dimensional array creation and setting elements is something basic that you should have learned while learning C#. What book or tutorial were you using to learn C#? I see that you have already progressed past console programs to using WinForms. One usually learns about variables and arrays while still doing console programs.

 
Last edited:
You are definitely right about progress of mine but sometimes we all look for birds fly not into our mouth ready roasted. But I got a sense of doing this case on my own after i read your reply. Thanks for your reply
 
So this is it, with my own.

C#:
var std = new List<Students>();
std.Add(new Students() { ID = "10", Name = "Karna", Surname = "Basalan", PassMark = "A" });
std.Add(new Students() { ID = "20", Name = "Kendar", Surname = "Nurwidiyanti", PassMark = "A" });

string[,] array2 = new string[2, 4];

for (int i = 0; i <= std.Count-1; i++)
{
    array2[i, 0] = std.ElementAt(i).ID.ToString();
    array2[i, 1] = std.ElementAt(i).Name.ToString();
    array2[i, 2] = std.ElementAt(i).Surname.ToString();
    array2[i, 3] = std.ElementAt(i).PassMark.ToString();
}
 
Good job for figuring it out.

See this cleaner approach:
C#:
var std = new List<Students>();
std.Add(new Students() { ID = "10", Name = "Karna", Surname = "Basalan", PassMark = "A" });
std.Add(new Students() { ID = "20", Name = "Kendar", Surname = "Nurwidiyanti", PassMark = "A" });

string[,] array2 = new string[student.Count, 4];

for (int i = 0; i < std.Count; i++)
{
    var student = std[i];
    array2[i, 0] = student.ID;
    array2[i, 1] = student.Name;
    array2[i, 2] = student.Surname;
    array2[i, 3] = student.PassMark;
}

Instead of calling ElementAt(), just use the list indexer to write more idiomatic C# code instead of looking like some one translating Java into C#.

As you progress through your learning curve in programming, learning to take a reference or copy of a repeatedly accessed object often aids the next person reading the code in understanding it, sometimes helps the compiler generate better code, and best of all, minimizes potential typos.

There is no need to call ToString() when the type of the field or variable is already a string.

The change in the for loops condition is to make the code more like idiomatic C#, C, C++, Java where the preference is to do the less than comparison instead of less than or equal when writing a for loop.
 
Last edited:
There is something important in this case that whether int type or numeric type exist in class object that we want to convert to string multidimensional array. It necessary to use .ToString() to get successful result if numeric data type exist in class object. Otherwise compiler does not like us :)
 
Yup. That is why I said:
There is no need to call ToString() when the type of the field or variable is already a string.
I was referring to the source, not the destination.
 

Latest posts

Back
Top Bottom