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:
string[][] test = new string[server1.GetResult().Length][];
test = Gronforum.GetResult();
You first assign a new array to test
, then overwrite it with the return value from GetResult()
. The code does the same as:
string[][] test = Gronforum.GetResult();
Now the GetResult()
should return a string[][]
- try this to get a feel of working with jagged arrays:
public string[][] GetResult()
{
string[][] result = new string[2][];
result[0] = new string[] { "1", "2" };
result[1] = new string[2];
result[1][0] = "a";
result[1][1] = "b";
return result;
}
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[][]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.