none
Method returning bool problem RRS feed

  • Question

  • I´m just starting to learn C# and have encountered  a problem with a method I´m creating (just for fun to learn). Still in console programming, so my skill level is not so high.

    class Program
        {
            static bool IsBoiling(int temp)
            {
                if (temp < 100)
                {
                    return false;
                }
            }
            static void Main(string[] args)
            {
                Console.Write("Degrees in celcius: ");
                string str = Console.ReadLine();
                int temp = Convert.ToInt32(str);
                if (IsBoiling(temp))
                {
                    Console.WriteLine("Water is boiling");
                }
                else
                {
                    Console.WriteLine("Water is not boiling");
                }
            }
        }

    This code returns an error in visual studio and I can not find the problem.

    Error CS0161 'Program.IsBoiling(int)': not all code paths return a value

    What am I doing wrong?


    Wednesday, January 30, 2019 11:41 AM

All replies

  • Error CS0161

    'Program.IsBoiling(int)': not all code paths return a value

    What am I doing wrong?

    As the error says : you must return a value if temp >= 100, like :

            static bool IsBoiling(int temp)
            {
                if (temp < 100)
                {
                    return false;
                }
                return true;
            }

    • Proposed as answer by Samuel.diogo Wednesday, January 30, 2019 12:04 PM
    Wednesday, January 30, 2019 11:48 AM
  • The problem is the method should return bool and let's assume the if condition fails in that case what it should return, as it's return type bool it should return either true or false. You need to fix your method to return a value:

    static bool IsBoiling(int temp) { var isBoiling = true;

    if (temp < 100) { isBoiling = false; } return isBoiling; }


    Hope it helps.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by Ehsan Sajjad Wednesday, January 30, 2019 12:02 PM
    Wednesday, January 30, 2019 12:02 PM
  • Hello,

    Here is a simplified version that will work. Ternary operator.

    static bool IsBoiling(int temp) => temp >= 100;


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 30, 2019 12:02 PM
    Moderator
  • After way to much time looking at this code, I finally found whats wrong. I left out the return true; The correct code should be like this

    class Program
        {
            static bool IsBoiling(int temp)
            {
                if (temp < 100)
                {
                    return false;
                }

             return true;
            }
            static void Main(string[] args)
            {
                Console.Write("Degrees in celcius: ");
                string str = Console.ReadLine();
                int temp = Convert.ToInt32(str);
                if (IsBoiling(temp))
                {
                    Console.WriteLine("Water is boiling");
                }
                else
                {
                    Console.WriteLine("Water is not boiling");
                }
            }
        }

    Wednesday, January 30, 2019 12:08 PM
  • You could also code it as shown below.

    • Using C#7, take advantage out var to declare the string to int.
    • Determine if the value is a integer.
    • Assert if not an integer or present the results.
    using System;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static bool IsBoiling(int temp) => temp >= 100;
            static void Main(string[] args)
            {
                Console.Write("Degrees in celsius: ");
                var str = Console.ReadLine();
                if (int.TryParse(str, out var temp))
                {
                    Console.WriteLine(IsBoiling(temp) ? 
                        "Water is boiling" : 
                        "Water is not boiling");
                }
                else
                {
                    Console.WriteLine($"{temp} must be numeric!");
                }
    
                Console.ReadLine();
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 30, 2019 12:58 PM
    Moderator
  • Very nice form, Kareninstructor.

    I'll use those tips in the future.

    Thursday, January 31, 2019 12:31 AM