Home

Built-In Structures: The Boolean Type

 

Introduction

As seen in previous lessons, the bool data type is used to represent a value considered as being true or false. In the .NET Framework, the bool data type is represented by the Boolean structure. The true value of a bool variable is represented by the TrueString field and the false value is represented by the FalseString member variable. In other words, when true (or false) is represented as a string, "true" (or "false" is the same as TrueString (or FalseString).

 

Parsing a Boolean Variable

We saw in a Lesson 8 that you could retrieve the value of a Boolean variable from a user. To support this, the Boolean structure is equipped with a static method named Parse. The Boolean.Parse() method is declared as follows:

public static bool Parse(string value);

This method takes as argument a string. The argument must contain either the word True (case-insensitive) or the word False. If the argument is passed as "True", the method returns true. If the argument is "false", this method returns false. Here is an example:

using System;

class Program
{
    static int Main()
    {
        bool result = bool.Parse("TRUE");

        Console.WriteLine("Result: {0}", result);
        return 0;
    }
}

This would produce:

Result: True
Press any key to continue . . .

When calling the Boolean.Parse() method to retrieve the value of a Boolean variable, if the supplied value is "TRUE" or "FALSE", the compiler would process it. If the value is not valid, the program would produce an error. Here is an example:

using System;

class Program
{
    static int Main()
    {
        bool HouseHas3Bedrooms = bool.Parse("ItHas3Bedrooms");

        Console.WriteLine("House has 3 bedrooms: {0}", HouseHas3Bedrooms);
        return 0;
    }
}

To avoid the error, the Boolean structure provides the TryParse() method. Its syntax is:

public static bool TryParse(string value, out bool result)

The first argument is the value to be parsed. If that value is valid, the second argument holds the True or False value of the first. Here is an example:

using System;

class Program
{
    static int Main()
    {
        bool alt;
        bool HouseHas3Bedrooms = bool.TryParse("True", out alt);

        Console.WriteLine("House has 3 bedrooms: {0}", HouseHas3Bedrooms);
        Console.WriteLine("Alternate value: {0}", alt);
        return 0;
    }
}

This would produce:

House has 3 bedrooms: True
Alternate value: True
Press any key to continue . . .

Consider this other version of the same program:

using System;

class Program
{
    static int Main()
    {
        bool alt;
        bool HouseHas3Bedrooms = bool.TryParse("False", out alt);

        Console.WriteLine("House has 3 bedrooms: {0}", HouseHas3Bedrooms);
        Console.WriteLine("Alternate value: {0}", alt);
        return 0;
    }
}

This would produce:

House has 3 bedrooms: True
Alternate value: False
Press any key to continue . . .

Notice that the first argument returns True although it was passed as False. This means that, if the value of the first argument is valid, it is the second argument, not the first, that holds the result. If the first argument is not valid, the second argument returns a False value. Consider the following version of the program:

using System;

class Program
{
    static int Main()
    {
        bool alt;
        bool HouseHas3Bedrooms = bool.TryParse("Don't Know", out alt);

        Console.WriteLine("House has 3 bedrooms: {0}", HouseHas3Bedrooms);
        Console.WriteLine("Alternate value: {0}", alt);
        return 0;
    }
}

This would produce:

House has 3 bedrooms: False
Alternate value: False
Press any key to continue . . .

Comparisons of Boolean Variables

In C#, to compare the values of two Boolean variables for equality, you can use the equality operator "==". Here is an example:

using System;

class Program
{
    static int Main()
    {
        bool House1Has3Bedrooms = true;
        bool House2Has3Bedrooms = false;

        Console.WriteLine("House1 has 3 bedrooms: {0}", House1Has3Bedrooms);
        Console.WriteLine("House2 has 3 bedrooms: {0}", House2Has3Bedrooms);
        Console.WriteLine("House1 and House2 have the number of bedrooms: {0}",
            House1Has3Bedrooms == House2Has3Bedrooms);
        return 0;
    }
}

This would produce:

House1 has 3 bedrooms: True
House2 has 3 bedrooms: False
House1 and House2 have the number of bedrooms: False
Press any key to continue . . .

To support this comparison, the Boolean structure of the .NET Framework is equipped with the Equals() method. Its syntax is:

public bool Equals(bool obj);

This method takes one argument as the Boolean value or variable to be compared to the variable that called it. Here is an example of calling it:

using System;

class Program
{
    static int Main()
    {
        bool House1Has3Bedrooms = false;
        bool House2Has3Bedrooms = false;

        Console.WriteLine("House1 has 3 bedrooms: {0}", House1Has3Bedrooms);
        Console.WriteLine("House2 has 3 bedrooms: {0}", House2Has3Bedrooms);
        Console.WriteLine("House1 and House2 have the number of bedrooms: {0}",
            House1Has3Bedrooms.Equals(House2Has3Bedrooms));
        return 0;
    }
}

This would produce:

House1 has 3 bedrooms: False
House2 has 3 bedrooms: False
House1 and House2 have the number of bedrooms: True
Press any key to continue . . .

The Equals() method can easily be called by one Boolean variable that receives another Boolean variable as argument. If you don't know the exact type of value of the argument, you can still pass it an object value. To support this, the Equals() method has another version whose syntax is:

public override bool Equals(Object obj);

This version takes as argument a variable that is of any type.

Besides the equality, you can also compare two Boolean variables to know whether one is lower than the other. To support this, the Boolean structure is equipped with the CompareTo() method. Its syntax is:

public int CompareTo(bool value);

If the type of variable is not necessarily a Boolean type, you can pass it as an object value. To do this, you can use the other version of the CompareTo() method. Its syntax is:

public int CompareTo(Object obj);
 

Home Copyright © 2006 FunctionX, Inc.