The Boolean data type stores True/False values. Boolean variables are, in essence, integers that take the value −1 (for True) and 0 (for False). Actually, any nonzero value is considered True. Boolean variables are declared as
Dim failure As Boolean
Code language: VB.NET (vbnet)
and they are initialized to False. Boolean variables are used in testing conditions, such as the following:
Dim failure As Boolean = False
' other statements ...
Code language: VB.NET (vbnet)
If failure Then MsgBox(”Couldn’t complete the operation”)
They are also combined with the logical operators And, Or, Not, and Xor. The Not operator toggles the value of a Boolean variable. The following statement is a toggle:
running = Not running
Code language: VB.NET (vbnet)
If the variable running is True, it’s reset to False, and vice versa. This statement is a shorter way of coding the following:
Dim running As Boolean
If running = True Then
running = False
Else
running = True
End If
Code language: VB.NET (vbnet)
Boolean operators operate on Boolean variables and return another Boolean as their result. The following statements will display a message if one (or both) of the variables ReadOnly and Hidden are True (presumably these variables represent the corresponding attributes of a file):
If ReadOnly Or Hidden Then
MsgBox("Couldn't open the file")
Else
{ statements to open and process file}
End If
Code language: VB.NET (vbnet)
The condition of the If statement combines the two Boolean values with the Or operator. If one or both of them are True, the parenthesized expression is True. This value is negated with the Not operator, and the If clause is executed only if the result of the negation is True. If ReadOnly is True and Hidden is False, the expression is evaluated as
If Not (True Or False)
Code language: VB.NET (vbnet)
(True Or False) is True, which reduces the expression to
If Not True
Code language: VB.NET (vbnet)
which, in turn, is False.
String Variables in Visual Basic 2008
The String data type stores only text, and string variables are declared as follows:
Dim anyText As String
Code language: VB.NET (vbnet)
You can assign any text to the variable anyText. You can store nearly 2GB of text in a string variable (that’s 2 billion characters, and is much more text than you care to read on a computer screen). The following assignments are all valid:
Dim aString As String
aString = "Now is the time for all good men to come " &
" to the aid of their country"
aString = ""
aString = "There are approximately 25,000 words in this chapter"
aString = "25,000"
Code language: VB.NET (vbnet)
The second assignment creates an empty string, and the last one creates a string that just happens to contain numeric digits, which are also characters. The difference between these two variables is that they hold different values:
Dim aNumber As Integer = 25000
Dim aString As String = "25,000"
Code language: VB.NET (vbnet)
The aString variable holds the characters 2, 5, comma, 0, 0, and 0; and aNumber holds a single numeric value. However, you can use the variable aString in numeric calculations, and the variable aNumber in string operations. VB will perform the necessary conversions as long as the Strict option is off.
The String data type and its text manipulation methods are discussed in detail in Chapter “Handling Strings, Characters, and Dates”.