0

我有一个函数,它使用 2D 锯齿状数组来保存 SQL 查询中的记录。

如何正确返回锯齿状数组?

我试过类似的东西:

public string[][] GetResult()
{
    return result;
}

在我的主程序中:

string[][] test = new string[server1.GetResult().Length][];
test = server1.GetResult();

好吧,正如预期的那样,它没有用。

我不知道如何解决我的问题。

4

1 回答 1

1

锯齿状数组只是数组的数组。

在您的代码中:

string[][] test = new string[server1.GetResult().Length][];
test = Gronforum.GetResult();

您首先将一个新数组分配给test,然后用 的返回值覆盖它GetResult()。代码与以下内容相同:

string[][] test = Gronforum.GetResult();

现在GetResult()应该返回一个string[][]- 试试这个来感受一下使用锯齿状数组:

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;
}

您可以提供对该方法的 SQL 操作结果的引用,以便它可以访问数据,将其“转换”为string[][].

于 2011-09-15T12:16:42.663 回答