如何“走出去”字符串数组并将其传递到列表框中
这是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在调用之前添加该变量的声明:
另一种选择是更改方法的签名并返回值,因此您可以这样写
You have to add the declaration of that variable before the call:
The other oppinion is to change the signature of your method and return the values, so you can write this
这听起来更像是代码设计问题。方法名称
GetSeatInfoStrings
清楚地表明它返回一些字符串。根据您对该方法的使用,它看起来像这样声明:在我看来,最好像这样声明它:
...并且像平常一样从方法中返回数组。
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:In my opinion, it would be far better to declare it like this instead:
...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. TheInt32.TryParse
method is a great example; the method returns abool
indicating success, and theout
parameter will contain the result.In your case it seems as if you have one result, so using
out
will only be confusing.