C# - Error “not all code paths return a value” with an array as out parameter

╄→尐↘猪︶ㄣ 提交于 2019-12-02 06:32:17

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;

    }

You have no final return before the method exits. You are exiting if there are no elements but you need a return at the end. If you are not interested in the value then why not set the return type to void?

You need to return an integer value according to your methods signature.

After the for loop is where a value should be returned.

The error isn't anything to do with your out parameter.

Your method

public int GetSeatInfoString(
                   DisplayOptions choice, out string[] strSeatInfoStrings)

is declared as returning an int, and doesn't do this for all code paths.

You are only returning a value if count <= 0. Either you need to return a value after the for loop, or change the method signature to be void, depending on what you want the return value to represent.

If you want to return the array with the counts in, then change the return type to string[] and remove the out argument.

What value do you want to return from this function? I guess that you need to add this line to the end:

return i;

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!