C# - 错误“并非所有代码路径都返回值";以数组作为输出参数 [英] C# - Error "not all code paths return a value" with an array as out parameter

查看:17
本文介绍了C# - 错误“并非所有代码路径都返回值";以数组作为输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有以下代码:

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

m_nameList[index].ToString(); }

    }

此代码产生错误...GetSeatInfoString.DisplayOptions, out string[])":并非所有代码路径都返回一个值.基本上,我希望在上述方法中执行的操作是循环一个数组和数组中包含字符串的任何值,我希望将它们添加到新数组中,strSeatInfoStrings 反过来可以从单独的类中调用,然后新的数组内容显示在列表框中.

This code produces an error of, "...GetSeatInfoString.DisplayOptions, out string[])': not all code paths return a value. Basically, what I am looking to do in the above method is to cycle through an array and for any values in the array that contain a string, I want these then adding to the new array, strSeatInfoStrings which in turn, can be called from a separate class and the new array content then displayed in a listbox.

关于如何纠正此问题的任何建议?

Any suggestions on how to rectify this?

提前致谢

推荐答案

可以在末尾加上 return strSeatInfoStrings.Length

You can add return strSeatInfoStrings.Length at the end

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;

    }

这篇关于C# - 错误“并非所有代码路径都返回值";以数组作为输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆