Reputation: 23
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!
Upvotes: 0
Views: 717
Reputation: 1241
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);
Upvotes: 2
Reputation: 465
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");
}
Upvotes: 2
Reputation: 887777
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"
.
Upvotes: 1