Extract methods with return statements

1.1k Views Asked by At

I am trying to refactor some code by extracting methods. I need to separate the (simplified) code below into sub methods for each region, but I get the error "When the selection contains a return statement, all code paths must be terminated by a return statement too".

try
        {
            #region one
            if (order == "1")
            {
                ...do something
                return result;
            }
            #endregion

            #region two
            if (order == "2")
            {
                ...do something
                return result;
            }
            #endregion

        }
        catch (Exception ex)
        {
            ...exception handle
        }
        return result;

How should this be done?

1

There are 1 best solutions below

1
On

You need to extract the body of each if statement into its own method. You cannot extract the entire if statement into a method, for exactly the reason the error message you're getting is telling you.