如何“走出去”字符串数组并将其传递到列表框中

发布于 2024-12-16 11:47:27 字数 430 浏览 2 评论 0原文

这是 UpdateGUI() 的一部分:

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

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

seatMngr.GetSeatInfoStrings(choice, out strSeatInfoStrings);

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

有什么想法吗?

This is a part of UpdateGUI():

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

The compiler is complaining about this row (And the parameter of the last row):

seatMngr.GetSeatInfoStrings(choice, out strSeatInfoStrings);

What I'm trying to do is to take an array (strSeatInfoStrings) and put it inside a listbox.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2024-12-23 11:47:27

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

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

You have to add the declaration of that variable before the call:

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

The other oppinion is to change the signature of your method and return the values, so you can write this

DisplayOptions choice = (DisplayOptions)comboBox1.SelectedIndex;
listBox1.Items.Clear(); 
listBox1.Items.AddRange(seatMngr.GetSeatInfoStrings(choice)); 
战皆罪 2024-12-23 11:47:27

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

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

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

public void IEnumerable<string> GetSeatInfoStrings(string choice)

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

在您的情况下,似乎您只有一个结果,因此使用 out 只会造成混乱。

This smells more like a code design issue. The method name GetSeatInfoStrings clearly expresses that it returns some strings. Based on your use of the method, it looks like it's declared like so:

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

In my opinion, it would be far better to declare it like this instead:

public void IEnumerable<string> GetSeatInfoStrings(string choice)

...and just return the array from the method as you normally would. The primary uses for out, as I see it is when you need to return more than one value from the method. The Int32.TryParse method is a great example; the method returns a bool indicating success, and the out parameter will contain the result.

In your case it seems as if you have one result, so using out will only be confusing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文