Error CS0161 not all code paths return a value
Error CS0161 not all code paths return a value
(OP)
I'm playing around with a class library in Visual Studio, integrating ExcelDNA to make a (few) User Defined Function for Excel.
The only error I am receiving is for the top "public static string StrCase" statement, but I haven't been able to figure out what the problem is. What do I need to do to get rid of this error?...what am I missing?
Error CS0161 not all code paths return a value
...I'm completely new to C#, by the way. Thanks!
The only error I am receiving is for the top "public static string StrCase" statement, but I haven't been able to figure out what the problem is. What do I need to do to get rid of this error?...what am I missing?
Error CS0161 not all code paths return a value
...I'm completely new to C#, by the way. Thanks!
CODE --> C#
public static string StrCase ( [ExcelArgument(Name="String",Description ="Text string that is to have letters' case change.")] string str, [ExcelArgument(Name ="Case",Description ="0 = no case change, 1 = change to UPPERCASE, 2 = change to lowercase, 3 = Change To Title Case")] byte bCase = 0 ) { if (bCase <= 0 || bCase > 3) // "||" is the C# OR operator { return str; } else { TextInfo strTI = new CultureInfo("en-US", false).TextInfo; if (bCase != 1) { return str.ToLower(); } if (bCase != 2) // "!=" is the equivalence operator { return str.ToUpper(); } if (bCase != 3) { str = strTI.ToTitleCase(str); return str; } } }
RE: Error CS0161 not all code paths return a value
1)
CODE
2) If bcase is 1, the bcase != 2 is satisfied
if bcase is 2, the bcase != 1 is satisfied
if bcase is 3, the bcase != 1 is satisfied
it never gets to bcase != 3.
It is better if you check for equality instead of inequality
RE: Error CS0161 not all code paths return a value
I'm an idiot. I was overly tired and misread C# documentation regarding equality comparison.
I corrected mistakes and compiled without a problem, but I was getting a weird error from Excel.
I tracked it down to one of the argument definitions and changed it from "byte" to "int" and then it ran okay. Any idea why Excel was rejecting the byte variable?
CODE --> C#