1

I currently have the below code:

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

    }

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?

Thanks in advance

1
  • 2
    Well, if you're not actually intending to return an int from the method, you could mark it as "void" instead. That should fix the immediate error. Commented Aug 1, 2011 at 13:26

6 Answers 6

3

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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;

    }

4 Comments

Many thanks folks for the answers thusfar. Basically, this method should return an array of strings in which each element is a string that is obtained from the array, m_nameList. The data within this array can then be populated into a listbox using the AddRange utility.
I do also have an enum method, DisplayOptions with the variable, choice, representing the three values in the method. This enum is listed on my GUI and what I am looking for is, is the user chooses to display entries from the array containing information, the method being discussed will cycle through the array and parse out all those entries that aren't empty. Similarly, if the user chooses from the enum combobox to display all entries, I'd like for the above method to display all entries from the array.
I think this is where the problem with the integer aspect lies as I believe my integer may represent the enum value.
@Enverlap - What? If you want to return an array of strings then set the method to string[] not int.
0

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

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

Comments

0

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.

Comments

0

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.

Comments

0

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

return i;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.