3.3. The Boolean Data Type

Of the two fundamental types of data, numeric and textual, thus far I have limited the discussion to numeric data types. (You will read more about textual data types in Chapter 6.) The Boolean data type is one of those gray areas that don't exactly fit into either a numeric or textual data type. While it is not a precise fit, I discuss the bool data type with the numeric data types because it is used to reflect the state of a piece of data, rather than some textual representation of that data.

One of the elemental features of a computer is that it can make decisions based upon the data it processes. You have seen this decision-making ability many times in our sample programs. For example, in the integer-division program you wrote earlier in this chapter, you had the following program statements:

flag = int.TryParse(txtOperand1.Text, out operand1);
if (flag == false)
{
  MessageBox.Show("Enter a whole number", "Input Error");
  txtOperand1.Focus();
  return;
}

The variable named flag that was set by the call to TryParse() was used to decide whether the user had typed in a valid integer number into the Text property of the txtOperand1 textbox object. If the user input could be converted successfully into an integer value, flag was set to true and the value of operand1 was set to the value entered by the user. If the user entered text that could not be converted to an integer (such as the letter o instead of the zero digit character, 0), flag was set to false ...

Get Beginning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.