Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error CS0161 not all code paths return a value 1

Status
Not open for further replies.

justhumm

Technical User
Nov 30, 2018
6
US
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!

Code:
         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;
                        }
                    }
                }
 
Two problems.
1)
Code:
    if ...
    else
    {
        if ... return
        if ... return
        if ... return
        // Compiler complaining out what if none of the above conditions are satisfied.
    }

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
 
Thanks, xwb...

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?

Capture_yd5hec.png


Code:
        [ExcelFunction(Description = "Changes the case of the letters in a text string.")]
        public static string ChangeCase
            ([ExcelArgument(Name="String",Description ="Text string that is to have the case of letters changed.")]
              string ChangeStr,
             [ExcelArgument(Name ="Case",Description ="0 = no case change, 1 = change to UPPERCASE, 2 = change to lowercase, 3 = Change To Title Case")]
              int bCase = 0
              // Byte bCase = 0
            )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top