Reputation: 17
I am pretty new to coding and I am getting this error ("not all code paths return a value") with the code below. Any help will be appreciated. Thanks
private int SelectCourse(string message)
{
int len = _courses.Count;
int index = -1;
if (len > 0)
{
for (index = 0; index < len; ++index)
{
Console.WriteLine($"{index + 1}. {_courses[index].Title}");
}
Console.Write(message);
string selection = Console.ReadLine();
while (!int.TryParse(selection, out index) || (index < 1 || index > len))
{
Console.Write("Please make a valid selection: ");
selection = Console.ReadLine();
}
--index;
}
--index;
}
Upvotes: 1
Views: 137
Reputation: 1292
When you write the signature of the function as follows:
private int SelectCourse(string message)
, you are making an agreement with the compiler that this function is guaranteed to return an integer. Given that there are no return statements in your function implementation, the compiler is complaining because you have broken the agreement.
Two suitable solutions are either return the index at some point (IE return index
at appropriate places in your code) OR make the function a void type which does not allow any data to be returned at all (IE private void SelectCourse(string message)
)
Upvotes: 2
Reputation: 163
You should have return value.
private int SelectCourse(string message)
{
int len = _courses.Count;
int index = -1;
if (len > 0)
{
for (index = 0; index < len; ++index)
{
Console.WriteLine($"{index + 1}. {_courses[index].Title}");
}
Console.Write(message);
string selection = Console.ReadLine();
while (!int.TryParse(selection, out index) || (index < 1 || index > len))
{
Console.Write("Please make a valid selection: ");
selection = Console.ReadLine();
}
--index;
}
--index;
//you should have a return value here
}
Upvotes: 1
Reputation: 959
This part of code does not return a type int variable. So you have to add the return
command Like this return --index;
in every possible returning of the index
value. In case you don't want to return something you have to change the declaration of your method from:
This---->private int SelectCourse(string message)
to
This ---->private void SelectCourse(string message)
Upvotes: 0
Reputation: 1784
As simple as, you have mentioned a return type in the method signature but in the method code, one/more paths not doing/giving any return value. So make sure you have specified "return something;" in all the execution paths possible in your method's code.
Upvotes: 1
Reputation: 193
Refer to: c# returning error "not all code paths return a value"
Every way the code can possible "go" it must eventually return some value. If you do not want it to return a value change your header from
private int SelectCourse(string message)
to
private void SelectCourse(string message)
Upvotes: 2
Reputation: 4444
When you define a method, you basically declare the elements of its structure. The syntax for defining a method in C# is as follows:
<Access Specifier> <Return Type> <Method Name>(Parameter List)
{
Method Body
}
Return type: A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void.
In your example method looks like this:
private int SelectCourse(string message)
as you can see access specifier is private, and return type is an integer, so that basically means that your method needs/must to return a value of type int
.
So to solve your issue, you need to put a :
return --index;
just before last curly bracket, because index
is type of int
as your method return type is, and there will be no issues anymore.
Upvotes: 5
Reputation: 8079
If index is the value you want to return, this will do it:
private int SelectCourse(string message)
{
int len = _courses.Count;
int index = -1;
if (len > 0)
{
for (index = 0; index < len; ++index)
{
Console.WriteLine($"{index + 1}. {_courses[index].Title}");
}
Console.Write(message);
string selection = Console.ReadLine();
while (!int.TryParse(selection, out index) || (index < 1 || index > len))
{
Console.Write("Please make a valid selection: ");
selection = Console.ReadLine();
}
return --index;
}
return --index;
}
Upvotes: 2
Reputation: 2516
You never return in your function and it should return an int
. An option would be to set the return type to void
so you don't have to return anything. But if your function really needs to return something then you have to fix your code and decide where you want to return (and what).
Upvotes: 2