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>>

Data Type Conversions

If an arithmetic operation in an expression has two operands that are of different data types, VB will have to perform conversion to make them the same type before it can proceed with the computation. In addition, if the result of the expression has a different data type from that of the variable on the left side of the assignment statement, VB will also make a conversion before moving the result to the variable. For example, consider the following code:

Dim I As Integer I = .5 * 20

There will be two conversion operations in executing the preceding code. In the code, .5 is a floating point constant; 20 is an integer constant. To make the two operands compatible, the number with smaller range or precision will be converted to the one with higher range and precision. Before the multiplication operation, 20 is converted to a Double floating point number. The multiplication is then carried out in a Double floating point operation. The result is a Double floating point value (10), which is then converted back to an integer number before it can be stored in the Integer variable, I.

As pointed out previously in this chapter, while VB will carry out these conversions automatically by default, it is advisable that you write your code explicitly to take care of the data conversions for semantic clarity and error minimization.

Last change: February 13 2016 18:47:36.
<<PreviousNext>>