No Return from Method???

ellpee

Active member
Joined
Apr 16, 2023
Messages
27
Programming Experience
10+
Why does this tell me not all code paths return a value? Seems to me it would return false every time it is called.

C#:
//Method to handle no entry by user....
            bool Check4NoInput(string dirtystring)
            {
                if (dirtystring == "" || dirtystring == null)
                {
                    Console.WriteLine("ERROR: No value was entered.  Please try again.");
                    return false;
                }
            }
 
Last edited by a moderator:
Really? What happens when dirtystring is "Dancing"?
 
:ROFLMAO: too! But hopefully somebody can tell me what the code editor is complaining about. The only thing this method is supposed to do is make sure the user entered SOMETHING. A second method then tests whatever was entered to make sure it's what my program needs.
 
Hopefully this graphic will make it clear what the compiler is complaining about:
1683733253740.png
 
Aha! So I need the "or" thingie between the two if tests, or alternatively, !> "". Will play with that a bit. Thanks!
 
Solution
Sort of...

Basically, if line 4 is false, then execution will jump to line 9. But line 9 is the end of the method. The compiler is expecting you to return something at the end of the method. (If line 4 is true, then execution will go to line 6, and then line 7. Line 7 is the a code execution path that end the method and that one returns false.)
 
You don't need this method; Microsoft already wrote it for you, in string.IsNullOrEmpty(the_user_input_here) - you just need to negate its output
 
And eventually, the universe will produce a better idiot, and the idiot user will type in a space to bypass the null or empty string check. So you'll need to move on the newer method added in the framework: string.IsNullOrWhiteSpace(). :)
 
Back
Top Bottom