Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何";“出去”;字符串数组,并将其传递到列表框中_C#_Arrays_Listbox_Out - Fatal编程技术网

C# 如何";“出去”;字符串数组,并将其传递到列表框中

C# 如何";“出去”;字符串数组,并将其传递到列表框中,c#,arrays,listbox,out,C#,Arrays,Listbox,Out,这是UpdateGUI()的一部分: 编译器正在抱怨此行(以及最后一行的参数): 我试图做的是获取一个数组(strSeatingFoString)并将其放入一个列表框中 有什么想法吗 您必须在调用之前添加该变量的声明: DisplayOptions choice = (DisplayOptions)comboBox1.SelectedIndex; string[] strSeatInfoStrings; seatMngr.GetSeatInfoStrings(choice, out strSea

这是UpdateGUI()的一部分:

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

我试图做的是获取一个数组(strSeatingFoString)并将其放入一个列表框中


有什么想法吗

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

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

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

public void GetSeatInfoStrings(string choice, out string[] result)
在我看来,最好是这样宣布:

public void IEnumerable<string> GetSeatInfoStrings(string choice)
public void IEnumerable GetSeatInfo字符串(字符串选项)
…然后像通常一样从方法返回数组。主要用于
out
,正如我看到的,当您需要从方法返回多个值时。
Int32.TryParse
方法就是一个很好的例子;该方法返回一个指示成功的
bool
,并且
out
参数将包含结果


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

是否有任何特殊原因需要使用
out
而不仅仅是从方法返回数组?您从编译器得到了什么错误消息?是的,我需要使用out。。。我得到的错误消息是:#1“名称'strSeatInfoStrings'在当前上下文中不存在”#2“与'Assignment3.SeatManager.GetSeatInfoStrings(Assignment3.DisplayOptions,out string[])匹配的最佳重载方法有一些无效参数”#3“参数2:无法从'out strSeatInfoStrings'转换为'out string[]”我已经在seatMngr对象内的GetSeatInfoStrings方法中清除了“string[]strSeatInfoStrings;”。当我尝试第一个建议时,我得到了“Nullreferenceexception未处理”,第二个建议得到了“错误1”“System.Windows.Forms.ListBox.ObjectCollection.AddRange(System.Windows.Forms.ListBox.ObjectCollection)的最佳重载方法匹配”'有一些无效参数C:\Users\Kalle\Desktop\Assignment4 hjälp\Assignment4\Assignment3\MainForm.cs 153 13 Assignment4”和一些其他问题。如果在方法
GetSeatInfoStrings
中声明它,则这是错误的范围。按照我在上面的示例中向您展示的方式声明它,只需在方法中使用out参数
GetSetInfoStrings
,您就会看到它是有效的。我这样做了,它给了我以下两个错误#1“使用未分配的out参数'strSeatInfoStrings'”#2“在控件离开当前方法之前,必须将out参数'strEATINFOSTRINGS'分配给”在离开方法的作用域之前,必须设置out参数的值。因此,如果存在if语句,并且仅在其中设置out参数,则还必须在else部分中对其进行初始化。
public void GetSeatInfoStrings(string choice, out string[] result)
public void IEnumerable<string> GetSeatInfoStrings(string choice)