I have a function which use a 2D jagged array to save records from an SQL query.
How do return the jagged array correctly?
I tried something like:
public string[][] GetResult()
{
return result;
}
And in my main programm:
string[][] test = new string[server1.GetResult().Length][];
test = server1.GetResult();
Well, as expected, it didn’t work.
I don’t know how to fix my problem.
Jagged arrays are simply arrays of arrays.
In your code:
You first assign a new array to
test, then overwrite it with the return value fromGetResult(). The code does the same as:Now the
GetResult()should return astring[][]– try this to get a feel of working with jagged arrays:You could supply a reference to the result of the SQL operation to that method so it has access to the data, to “convert” it to a
string[][].