- Home /
js switch case warning
I wrote a simple code below but I get a warning that is "assets/Scripts/Global.js(5,17): BCW0023: WARNING: This method could return default value implicitly."
#pragma strict
public static class Global{
public function checkValueOfTag(str_tag : String) : int{
switch (str_tag)
{
case "num0":
return 0;
case "num1":
return 1;
case "num2":
return 2;
case "num3":
return 3;
case "num4":
return 4;
case "num5":
return 5;
case "num6":
return 6;
case "num7":
return 7;
case "num8":
return 8;
case "num9":
return 9;
default:
return 0;
}
}
}
Have you tried this with the :
default:
return 0;
commented out to see if this warning disappears?
I found a solution. I changed to function this. It should work.
public function checkValueOfTag(str_tag : String) : int{
var result : int = 0;
switch (str_tag)
{
case "num0":
result = 0;
break;
case "num1":
result = 0;
break;
case "num2":
result = 0;
break;
case "num3":
result = 0;
break;
case "num4":
result = 4;
break;
case "num5":
result = 5;
break;
case "num6":
result = 6;
break;
case "num7":
result = 7;
break;
case "num8":
result = 8;
break;
case "num9":
result = 9;
break;
default:
result = 0;
break;
}
return result;
}
Answer by Kiwasi · Oct 25, 2014 at 10:56 PM
To explain the warning. The compiler is not sure if every path in your code returns a value. The warning is saying that there is a possibility that the function won't return a value, and the default will be used instead. The default for int is 0.
The typical way to deal with this is to add a return at the very end of your function. Note that this is a warning, not an error. You can ignore it if you choose. Your code in the comments does make the warning go away, but it's not the most efficient way to do so. Local variables will need to be allocated.
function DoStuff () : int {
// Stuff
return 0;
}
Your answer
Follow this Question
Related Questions
Switch case using enum javascript 2 Answers
Light switch trigger 3 Answers
Syntax of switch + case insensitive? 2 Answers
Play anim, javascript 1 Answer
Setting Scroll View Width GUILayout 1 Answer