Search code examples
c#if-statementreturn

C# return isn't letting user know of invalid entry


I have public string method with an if/else statement. When I don't put a return keyword on the else, I get the error that not all code paths return a value but when I add the return to the else statement, I get an Use of unassigned local variable 'result'. When I do return "Please enter a valid number";, nothing displays on the console when I type in any letter. I need to let the user know that they entered something in that couldn't be converted into an integer.

public string AddNumbers(string userInputString)
    {
        int result;
        int num2 = RandomNumberInt();
        bool isTrue = int.TryParse(userInputString, out int num1);

        if (isTrue == true)
        {
            result = num1 + num2;
            Console.WriteLine("Adding your number and a random number, please wait...");
            Thread.Sleep(1000);
            Console.WriteLine("{0} + {1} = {2}", num1, num2, result);
            isTrue = true;
            return result.ToString();
        }
        else
        {
            return "Please enter a valid number";
        }
    }

Solution

  • It's because your method is asking for a return. But since you have an if, let's put it this way

    Team Leader: Hey bob I want you to finish this job.

    In here you are expecting that bob will finish your job but you put a condition

    You: If I can go to office today sir.

    since you have an If condition, what if you can't go today? Does it mean you can't finish the job tomorrow and any other day? That's why it says not all code paths return a value because you never gave other/default value

    Going back in your code, you ask

        if (isTrue == true)
        {
            result = num1 + num2;
            Console.WriteLine("Adding your number and a random number, please wait...");
            Thread.Sleep(1000);
            Console.WriteLine("{0} + {1} = {2}", num1, num2, result);
            isTrue = true;
            return result.ToString();
        }
    

    but what if it is not true, then it will go to else. You can set int result = 0 so it is defined from start when else run and it will not return an error of unassigned local variable.

    public string myMethod()
    {
        int result = 0; //This must have a default value so in else part it is not unassigned when you return.
        int num2 = RandomNumberInt();
        bool isTrue = int.TryParse(userInputString, out int num1);
    
        if(isTrue)
        {
            //do something in result here
            return result.toString(); //This will return your in value as string
        }
        else
        {
            return "Your message as string" // This will return any string to provide for string return of the method
        }
    }
    

    Your problem regarding not displaying the string "Please enter a valid number" is a different issue. You have to post also what method calls the AddNumbers method that will use the returned string. For use the problem is in that method because your if and else is in correct format;