The .NET Boolean type represents true or false. Variables of this type work well with the conditional statements that were just discussed. When you declare a variable of type Boolean, you can use it within a conditional statement directly. Test the following sample by creating a Sub called BoolTest within ProVB_VS2010:

Private Sub BoolTest()

Dim blnTrue As Boolean = True Dim blnFalse As Boolean = False If (blnTrue) Then

TextBoxl.Text = blnTrue & Environment.NewLine TextBoxl.Text &= blnFalse.ToString End If End Sub

Code snippet from Forml

The results of this code are shown in Figure 2-4. There are a couple things outside of the Boolean logic to review within the preceding code sample. These are related to the update of Textboxl.Text. In this case, because you want two lines of text, you need to embed a new line character into the text. There are two ways of doing this in Visual Basic. The first is to use the Environment.Newline constant, which is part of the core .NET Framework. Alternatively, you may find a Visual Basic developer leveraging the Visual Basic-specific constant vbCRLF, which does the same thing.

The second issue related to that line is that I am concatenating the implicit value of the variable blnTrue with the value of the Environment.Newline constant. Note the use of an ampersand FIGURE 2-4 (& ) for this action. This is a best practice in Visual Basic because while Visual Basic does overload the plus (+) sign to support string concatenation, in this case the items being concatenated aren't necessarily strings. This is related to not setting Option Strict to on) In that scenario, the system will look at the actual types of the variables and if there were two integers side by side in your string concatenation you would get unexpected results. This is because the code would first process the "+" and would add the values as numeric values.

Thus, since neither you nor the sample download code has set Option String to on for this project, if you replace the preceding & with a +, you'll find a runtime conversion error in your application. Therefore, in production code it is best practice to always use the & to concatenate strings in Visual Basic unless you are certain that both sides of the concatenation will always be a string. However, neither of these issues directly affect the use of the Boolean values, which when interpreted this way provide their ToString() output, not a numeric value.

Code snippet from Forml

Always use the True and False constants when working with Boolean variables.

Unfortunately, in the past developers had a tendency to tell the system to interpret a variable created as a Boolean as an Integer. This is referred to as implicit conversion and is related to Option Strict. It is not the best practice, and when .NET was introduced, it caused issues for Visual Basic because the underlying representation of True in other languages wasn't going to match those of Visual Basic. The result was that Visual Basic represents True differently for implicit conversions than other .NET languages.

True has been implemented in such a way that when converted to an integer, Visual Basic converts a value of True to -1 (negative one). This is one of the few (but not the only) legacy carryovers from older versions of Visual Basic, and is different from other languages, which typically use the integer value 1. Generically, all languages tend to implicitly convert False to 0, and True to a nonzero value.

However, Visual Basic works as part of a multilanguage environment, with metadata-defining interfaces, so the external value of True is as important as its internal value. Fortunately, Microsoft implemented Visual Basic in such a way that while -1 is supported, the .NET standard of 1 is exposed from Visual Basic methods to other languages.

To create reusable code, it is always better to avoid implicit conversions. In the case of Booleans, if the code needs to check for an integer value, then you should explicitly evaluate the Boolean and create an appropriate integer. The code will be far more maintainable and prone to fewer unexpected results.

0 0

Post a comment