×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Error CS0161 not all code paths return a value

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!

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

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

RE: Error CS0161 not all code paths return a value

(OP)
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?



CODE --> C#

[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
            ) 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close