- Home /
The question is answered, right answer was accepted
Error CS0161 : not all code paths return a value?
I came up with this error and I searched alot about this error and tryied diferent code options but I wasn't able to find my way arround it so here I am asking for your help.
Heres the code:
public string ReturnItemName (int index) {
for (int i = 0; i<ItemList.Length; i++) {
if (ItemList[i].Id == index) {
return ItemList[i].Name;
}
}
}
Thanks in advance!
What do you think "not all code paths return a value" means? I interpret that to mean that the compiler can see that you have a function called ReturnItemName
that returns a string
but some paths through the code don't return anything. I guess the compiler is saying that if your if ()
never gets to be true, the function can drop out of your for ()
and then not return any value.
Answer by getyour411 · Mar 29, 2014 at 07:10 PM
What happens when if(condition) is false? There's no "return;"
You need to restructure this to always return something
for(whatever)
{
if(blah == blah)
return something ;
}
return somethingElse ;
I tried that already, but I want it to return nothing, what can I do to return null?
I did
return;
and
return null;
neither had worked and continues to give me the error
put the return null outside of the for loop like I showed above, not inside the loop.
that did it man, I thought I had put it outside. Thank you so much :)
Follow this Question
Related Questions
Unity C# error with return 0 Answers
Not all code paths return a value? 2 Answers
LODGroup.GetLODs returns null 0 Answers
Returning item names in an inventory array 3 Answers
Can't return a string after value is true in coroutine 1 Answer