0

I am having a problem with my solution. I am trying to create an MVC application wherein the view has a textbox for a string, a dropdown list for sorting type(e.g bubble sort,quick sort) and a button to sort the string.

here is my code in controller:

[HttpPost]
    public ActionResult total(string value1, String calci)
    {
        char[] total;
        switch (calci)
        {
            case "bubbleSort":
                total = value1.ToCharArray();
                char temp;
                for (int write = 0; write < total.Length; write++)
                    for (int sor = 0; sor < total.Length - 1; sor++)
                        if (total[sor] > total[sor + 1])
                        {
                            temp = total[sor + 1];
                            total[sor + 1] = total[sor];
                            total[sor] = temp;
                        }
                return Content("Result " + total);

        }
    }

it keeps giving me an error of "not all code paths return a value" I really need to run this program, also ignore the "total" from my actionresult because my the original solution of this project is my calculator project.Thankyou!

4
  • could be wrong but I think you need a break; at the end of your case. Also a default case should be included. Commented Jul 24, 2017 at 17:59
  • @CapnJack: No; that isn't reachable because of the return. Commented Jul 24, 2017 at 17:59
  • Add the default case inside the switch, or a return at the end of your method. Commented Jul 24, 2017 at 18:00
  • @SLaks true, thanks. Commented Jul 24, 2017 at 18:01

3 Answers 3

2

You will need a default case such as this:

        switch (calci)
        {
            case "bubbleSort":
                total = value1.ToCharArray();
                char temp;
                for (int write = 0; write < total.Length; write++)
                for (int sor = 0; sor < total.Length - 1; sor++)
                    if (total[sor] > total[sor + 1])
                    {
                        temp = total[sor + 1];
                        total[sor + 1] = total[sor];
                        total[sor] = temp;
                    }
                return Content("Result " + total);
            default:
                return Content("Result 0");                    
        }
Sign up to request clarification or add additional context in comments.

Comments

2

Your issue is that you only have a return value in your switch statement. If the case is "bubbleSort" the there isn't anything to return! Whenever you have a method with a return type other than void, you need to ensure all possible routes have a return value of the type specified when creating your class.

You can resolve this by adding a default case statement that returns a result as well. Try adding this after your first case:

default:
    return Content(@"Unhandled 'calci' parameter: " + calci);

1 Comment

1

As the error is trying to tell you, you need to make sure that your function always returns a value.

Even if calci is not "bubbleSort".

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.