1

这是 UpdateGUI() 的一部分:

DisplayOptions choice = (DisplayOptions)comboBox1.SelectedIndex;
seatMngr.GetSeatInfoStrings(choice, out strSeatInfoStrings);
listBox1.Items.Clear();
listBox1.Items.AddRange(strSeatInfoStrings);

编译器抱怨这一行(以及最后一行的参数):

seatMngr.GetSeatInfoStrings(choice, out strSeatInfoStrings);

我要做的是获取一个数组(strSeatInfoStrings)并将其放入列表框中。

有任何想法吗?

4

2 回答 2

2

您必须在调用之前添加该变量的声明:

DisplayOptions choice = (DisplayOptions)comboBox1.SelectedIndex;
string[] strSeatInfoStrings;
seatMngr.GetSeatInfoStrings(choice, out strSeatInfoStrings); 
listBox1.Items.Clear(); 
listBox1.Items.AddRange(strSeatInfoStrings); 

另一种意见是改变你的方法的签名并返回值,所以你可以这样写

DisplayOptions choice = (DisplayOptions)comboBox1.SelectedIndex;
listBox1.Items.Clear(); 
listBox1.Items.AddRange(seatMngr.GetSeatInfoStrings(choice)); 
于 2011-11-17T10:29:54.403 回答
0

这闻起来更像是代码设计问题。方法名称GetSeatInfoStrings清楚地表明它返回一些字符串。根据您对该方法的使用,它看起来像这样声明:

public void GetSeatInfoStrings(string choice, out string[] result)

在我看来,最好这样声明它:

public void IEnumerable<string> GetSeatInfoStrings(string choice)

...并像往常一样从方法中返回数组。我看到的主要用途out是当您需要从方法中返回多个值时。该Int32.TryParse方法就是一个很好的例子;该方法返回一个bool指示成功,out参数将包含结果。

在您的情况下,您似乎只有一个结果,因此使用out只会令人困惑。

于 2011-11-17T10:40:29.323 回答