C# C“错误”;并非所有代码路径都返回一个值";使用数组作为输出参数
我目前拥有以下代码: public int GetSeatInfoString(显示选项选项,输出字符串[]streasatinfostrings) 此代码生成错误“…GetSeatInfoString.DisplayOptions,out string[])“:并非所有代码路径都返回值。基本上,我在上面的方法中要做的是循环遍历一个数组,对于数组中包含字符串的任何值,我希望将这些值添加到新数组中,然后可以从单独的类调用streAtinFoString,然后将新数组内容显示在列表框中 有没有关于如何纠正这种情况的建议C# C“错误”;并非所有代码路径都返回一个值";使用数组作为输出参数,c#,arrays,listbox,C#,Arrays,Listbox,我目前拥有以下代码: public int GetSeatInfoString(显示选项选项,输出字符串[]streasatinfostrings) 此代码生成错误“…GetSeatInfoString.DisplayOptions,out string[])“:并非所有代码路径都返回值。基本上,我在上面的方法中要做的是循环遍历一个数组,对于数组中包含字符串的任何值,我希望将这些值添加到新数组中,然后可以从单独的类调用streAtinFoString,然后将新数组内容显示在列表框中 有没有关于如
提前感谢在方法退出之前,您没有最终的返回。如果没有元素,您将退出,但您需要在结束时返回。如果您对该值不感兴趣,为什么不将返回类型设置为void?您需要根据方法签名返回一个整数值
for循环之后是返回值的位置。错误与
out
参数无关
你的方法
public int GetSeatInfoString(
DisplayOptions choice, out string[] strSeatInfoStrings)
声明为返回一个
int
,但不会对所有代码路径执行此操作。您可以在末尾添加returnstreastinfostrings.Length
public int GetSeatInfoString(DisplayOptions choice, out string[] strSeatInfoStrings)
{
strSeatInfoStrings = null;
int count = GetNumOfSeats(choice);
if ((count <= 0))
return 0;
strSeatInfoStrings = new string[count];
int i = 0;
for (int index = 0; index <= m_totNumOfSeats - 1; index++)
{
if (string.IsNullOrEmpty(m_nameList[index]))
strSeatInfoStrings[i++] =
m_nameList[index].ToString(); }
return strSeatInfoStrings.Length;
}
public int GetSeatInfoString(显示选项选项,输出字符串[]streasatinfostrings)
{
strStatinFoString=null;
int count=GetNumOfSeats(选项);
if((count如果count您只返回一个值,那么您希望从该函数返回什么值?我想您需要将这一行添加到末尾:
return i;如果您实际上不打算从该方法返回int,可以将其标记为“void”"取而代之的是。这应该可以修复即时错误。非常感谢大家的回答。基本上,这个方法应该返回一个字符串数组,其中每个元素都是从数组m_nameList中获取的字符串。然后可以使用AddRange实用程序将此数组中的数据填充到列表框中。我还有一个枚举方法DisplayOptions,变量choice,表示方法中的三个值。此枚举列在我的GUI上,我要查找的是,如果用户选择显示包含信息的数组中的条目,则讨论的方法将在数组中循环,并解析出所有非空的条目。类似地,如果用户从“枚举”组合框中选择显示所有条目,我希望上面的方法显示数组中的所有条目。我认为这就是整数方面的问题所在,因为我认为我的整数可能代表枚举值。@Enverlap-What?如果要返回字符串数组,请将方法设置为string[]而不是int。
public int GetSeatInfoString(
DisplayOptions choice, out string[] strSeatInfoStrings)
public int GetSeatInfoString(DisplayOptions choice, out string[] strSeatInfoStrings)
{
strSeatInfoStrings = null;
int count = GetNumOfSeats(choice);
if ((count <= 0))
return 0;
strSeatInfoStrings = new string[count];
int i = 0;
for (int index = 0; index <= m_totNumOfSeats - 1; index++)
{
if (string.IsNullOrEmpty(m_nameList[index]))
strSeatInfoStrings[i++] =
m_nameList[index].ToString(); }
return strSeatInfoStrings.Length;
}