-5

I want to do something like this -

using System;

class MainClass
{
    public static void Main ()
    {
        bool? input;
        Console.WriteLine ("Are you Major?");
        input = bool.Parse (Console.ReadLine ());
        IsMajor (input); 

    }


    public static void IsMajor (bool? Answer)
    {
        if (Answer == true) {
            Console.WriteLine ("You are a major");
        } else if (Answer == false) {
            Console.WriteLine ("You are not a major");
        } else {
            Console.WriteLine ("No answer given");
        }
    }

}

Here if user gives no answer and simply presses enter, the variable input must store the value null and output must be No answer given.

In my code, input of true and false is working fine.

But if no input is given and enter is pressed the compiler throws the exception

System.FormatExeption has been thrown
String was not recognized as a valid Boolean

So how to get null value stored in variable input so that output is No answer given

Here,

the question String was not recognized as a valid boolean C#

is obviosly not a duplicate as it does not want to take null input directly from keyboard. And if such input can't be taken, what is the utility of nullable type, as there would be work around as well?

Suraj
  • 724
  • 1
  • 9
  • 17
  • Read the console input into a string, then use bool.TryParse. – Equalsk Dec 28 '17 at 10:02
  • But that is a way around. Can direct null input be taken as should be in case of nullable data type? – Suraj Dec 28 '17 at 10:04
  • 3
    Possible duplicate of [String was not recognized as a valid boolean C#](https://stackoverflow.com/questions/40010816/string-was-not-recognized-as-a-valid-boolean-c-sharp) – mjwills Dec 28 '17 at 10:04
  • No, obviously not, otherwise your code would work already. This is exactly why TryParse exists. – Equalsk Dec 28 '17 at 10:05
  • @mjwills no it is not a duplicate, I want direct null input from keyboard – Suraj Dec 28 '17 at 10:06
  • @mjwills no it is not my answer, it has no mention of nullable data type – Suraj Dec 28 '17 at 10:16

3 Answers3

4
bool input;
Console.WriteLine("Are you Major?");
if (!bool.TryParse(Console.ReadLine(), out input))
{
    Console.WriteLine("No answer given");
}
else
{
    //....
}

Or using C# 7:

if (!bool.TryParse(Console.ReadLine(), out bool input))
{
    Console.WriteLine("No answer given");
}
else
{
    // Use "input" variable
}
// You can use "input" variable here too
JohnyL
  • 6,894
  • 3
  • 22
  • 41
  • Good answer, but I don't want to do it this way. What is the utility of nullable data type then? There will be workaround, when nullable data type is not used, as well. – Suraj Dec 28 '17 at 10:13
  • 1
    @Suraj For instance, in Entity Framework. When `bit` column in database allows `null`s (IsAttached bit null), then you must use `bool?`. – JohnyL Dec 28 '17 at 10:22
2
bool? finalResult = null;
bool input = false;

Console.WriteLine("Are you Major?");

if (bool.TryParse(Console.ReadLine(), out input))
    finalResult = input;
}

Using the above technique finalResult will be null if the input cannot be parsed as either true or false.

mjwills
  • 23,389
  • 6
  • 40
  • 63
-2

You could surround your parse with a try-catch, and on catch (so if something other than true or false is given by the user) set input to null.

Christophe Loeys
  • 196
  • 2
  • 11
  • That is a way around. Can directly null input can be taken as it should be in case of nullable data types? – Suraj Dec 28 '17 at 10:05
  • No this is not possible. If you don't enter anything, Console.ReadLine() will return the empty string, so you have to do something yourself with it (i.e. using String.IsNullOrEmpty()) – Christophe Loeys Dec 28 '17 at 10:09