Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 4: Data, Operations, and Built-In Functions
Last change: February 13 2016 18:47:36.
<<PreviousNext>>

Strict Implicit Type Conversion

The confusing results from using the Object type is partially caused by VB’s permissive data type conversion rules. By default, VB allows the programmer to write code without much concern about how data should be converted from one type to another. This convention appears to be convenient for the beginner to learn the language; however, it also can result in mysterious bugs in the program. The programmer might believe that the compiler does one thing, while it actually does another. The best way to avoid this kind of problem is to explicitly perform data conversion by code. For example, in the preceding example, if you mean for the expression to perform a string operation, you should code:

V = CStr(V) + CStr(45)

In the code, CStr is a function that converts the argument, V (and 45) into a string. Section 4.4, “Built-in Numeric Functions” presents a group of data conversion functions. Note that although at this point in the code V has functioned as a string, the CStr function is still needed under the strict conversion rule because V (being of the Object type) can be any data type at execution time.
You can have the complier force you to observe strict data conversion rules by adding a line of code at the beginning of the module:

Option Strict On

When the strict data conversion rule is on, you can implicitly convert data from a type of narrower range to that of a wider range, such as from Single to Double, but not the other way. In this case, if you really mean to do it, you will need to use one of the conversion functions explicitly. As you can infer, when your program does not have this line of code, you are in effect coding with Option Strict Off by default. It is recommended that you have this option on. Programs coded with explicit data type conversion code are easier to understand because the code explicitly shows the intended data type of each element, and is less error prone. You can have the compiler enforce strict data type conversion rules for the project without any code by setting Option Strict On in the Options dialog box. The steps are exactly the same as turning on Option Explicit, as shown in Figure 4-1. This textbook will follow strict data conversion rules as soon as you learn those data conversion functions in Section 4.4.

Try This
Place the following code in a button click event.
Dim V As Object V = "1.23" V = V + 45 MsgBox("V=" & V) MsgBox(V.GetType().ToString)
In the code, the GetType method returns the data type of the object. When you run the program and click the button, you should see in the message box that V = 46.23 and the data type is System.Double. Add the following line at the beginning of your code window (before any statement).
Option Strict On
Check the code in your Click procedure again. You should see V underlined. Change the code as follows and test the program again.
Dim V As Object V = "1.23" V = CStr(V) + CStr(45) MsgBox("V=" & CStr(V)) MsgBox(V.GetType().ToString)
What do you see this time? The errors disappear because your code observes the strict type rules. Comment out the Option Strict On statement before proceeding further.
Last change: February 13 2016 18:47:35.
<<PreviousNext>>