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

Conversion Functions

The following table lists selected data conversion functions.

Function Use Code Example Result
CBool(x) Returns a Boolean type value; all nonzero values converted to True CBool(1) True
CByte(x) Returns a Byte type value CByte(“12”) 12
CDate(x) Returns a Date/Time type value CDate(“31-Dec-98”) 12/31/1998 12:00:00 AM
CDbl(x) Returns a Double type value CDbl(“12.34”) 12.34
CDec(x) Returns a Decimal type value CDec(“12.34”) 12.34
CInt(x) Returns an Integer type value CInt(“12.34”) 12
CLng(x) Returns a Long type value CLng(“12.34”) 12
CShort(x) Returns a Short type value CShort(“12.34”) 12
CSng(x) Returns a Single type value CSng(“12.6”) 12.6

In all the conversion functions listed in the table, x is an expression that can be evaluated to a numeric value. These functions typically are used to convert from one numeric data type to another; however, they can also be used to convert strings that appear to human beings as numbers. For example, the expressions CLng(''3,456'') and CLng(''$3,456'') will both yield a result of 3456. The expression CLng('' '') will cause an error because a blank space is not considered a number.
You may have noticed that the preceding example program to compute net receipts behaves exactly as described here, although the content of the text box is assigned to the numeric variable directly without calling any conversion function. The Text property of the text box is of the String type; however, those variables are declared to be the Double type. As noted previously in this chapter, in the absence of Option Strict specification, when the data type of the expression (source) is different from the variable on the left side (target) of an assignment statement, a data conversion will occur. One of these conversion functions is automatically called.
Note also, the Int and Fix functions differ from CInt and CLng in that the former pair truncates the fractional portion from the result, and the latter round the number. Using the number 5.5 in Int or Fix will have a result of 5, but will have a result of 6 with CInt or CLng.

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