"Mixed mode" assignment statements were shown in a previous topic (i.e., a string was assigned to an integer, a single was assigned to a string, etc.). VB would perform the necessary conversions in these cases wherever it could. Such conversions are called implicit conversions.
In VB, you can also use a set of functions that explicitly convert (or "cast") one type of data to another. The set of functions that enable you to do this all begin with the letter "C": CBool, CByte, CCur, CDate, CDbl, CDec, CInt, CLng, CSng, CStr, and CVar. In addition, two older functions, Val and Str, enable you to convert from a string to a number and from a number to a string, respectively. Each function is described below:
CBool takes any valid expression and converts it to Boolean. Values of zero convert to False, all others to True. If the expression is String, it must be a string that can be converted to a number (like "123"). The strings "True" and "False" will also work; any other string will result in an error.
Examples:
blnTest = CBool(50) ' blnTest = True
blnTest = CBool(0) ' blnTest = False
blnTest = CBool("-26") ' blnTest = True
blnTest = CBool("True") ' blnTest = True
blnTest = CBool("False") ' blnTest = False
blnTest = CBool("ABC") ' *** Error ***
CByte converts an expression to a Byte data type. The value of the expression is rounded up, must not exceed 255, and must not be negative.
Examples:
bytX = CByte(125.5678) ' bytX = 126
bytX = CByte(300.99) ' error
CCur converts an expression to a Currency data type. The expression must be a number (or a string representation of a number) that is with the Currency range. The result is rounded off at the 4th digit after decimal point.
Examples:
curX = CCur(1234.56789) ' curX = 1234.5679
curX = CCur(1234.56782) ' curX = 1234.5678
CDate converts a numeric or string expression to a Date data type. (For numeric expressions, 0 represents the date 12/30/1899. Negative values represent dates before 12/30/1899; positive values represent dates after. The decimal portion of a number represents the time – for example, .5 = noon.) Numeric or string arguments outside of the range of the Date data type (1/1/100 thru 12/31/9999) results in an error.
Examples:
dtmTest = CDate("1/1/2000") 'dtmTest = 1/1/2000
dtmTest = CDate("January 1, 2000") 'dtmTest = 1/1/2000
dtmTest = CDate(123) 'dtmTest = 5/2/1900
dtmTest = CDate(123.456) 'dtmTest = 5/2/1900 10:56:38 AM
dtmTest = CDate("ABC") 'error
CDbl converts an expression to a Double data type. The expression must be a number (or a string representation of a number) that is within the Double range.
Example:
dblTest = CDbl("1058.930") 'dblTest = 1058.93
CDec converts an expression to a Decimal data type. The expression must be a number (or a string representation of a number) that is within the Decimal range (roughly, a 29 digit number with a varying decimal point).
Note: The Decimal data type is not a "stand alone" VB data type and can only be used with Variant data types.
The following example stores the Decimal representation of 123.456 in vntDecValue and tells VB to use the Decimal sub-type with the Variant variable vntDecValue:
vntDecValue = CDec("123.456")
CInt converts an expression to an Integer data type. The expression must be a number (or a string representation of a number) that is within the Integer range. Values with digits after the decimal point are automatically rounded to the nearest integer.
intX = CInt(-1.2) 'intX = -1
intX = CInt(-1.9) 'intX = -2
intX = CInt(3.69) 'intX = 4
intX = CInt(3.3) 'intX = 3
CLng converts an expression to a Long data type. The expression must be a number (or a string representation of a number) that is within the Long range. Values with digits after the decimal point are automatically rounded to the nearest integer.
Examples:
lngX = CLng(-1.2) 'lngX = -1
lngX = CLng(-1.9) 'lngX = -2
lngX = CLng(3.69) 'lngX = 4
lngX = CLng(3.3) 'lngX = 3
CSng converts an expression to a Single data type. The expression must be a number (or a string representation of a number) that is within the Single range.
Example:
sngTest = CSng("1058.930") 'sngTest = 1058.93
CStr converts a variable of any datatype to a string. If the argument is a Date, the "short format" date (m/d/yy) is returned. If the argument is Boolean, the string "True" or "False" is returned. If the argument is a number, a string representation of the number is returned.
Examples:
dtmTest = #08/02/2001#
blnTest = False
sngTest = 123.456
strTest = CStr(dtmTest) 'strTest = "8/2/01"
strTest = CStr(blnTest) 'strTest = "False"
strTest = CStr(sngTest) 'strTest = "123.456"
CVar converts a variable to a Variant. If the argument is numeric, the result is a Double; for non-numeric values, the result is a String.
The Val Function
The Val function is a more generic (and less efficient) way to convert a string to a number. Technically, it returns a Double data type. It strips all blanks from a string argument and then converts the remaining characters to a number. The Val function recognizes a leading sign, digits, and a decimal point as part of a number; it will stop scanning the string at the first character that cannot be considered part of a number. If the string cannot be converted to a number, 0 will be returned.
Examples:
lngX =Val("
1615 198th Street N.E.
") 'lngX = 1615198
intX =
Val("2457"
)'intX = 2457
intX =
Val(" 2 45 7"
)'intX = 2457
intX =
Val("24 and 57"
)'intX = 24
sngX = Val("-1234.56") 'sngX = -1234.56
sngX = Val("-1,234.56") 'sngX = -1
dblX = Val("ABC") 'dblX = 0
Although less efficient than the "Cxxx" functions, one advantage to using Val is that if the string argument cannot be converted to a number, Val will simply return 0, whereas the equivalent "Cxxx" function would return a Type Mismatch error.
The Str (or Str$) Function
The Str function is an older function (dating back to earlier versions of BASIC) that converts a number to a string. When numbers are converted to strings, a leading space is always reserved for the sign of number. If number is positive, the returned string contains a leading space and the plus sign is implied. The Str function recognizes only the period (.) as a valid decimal separator.
Examples:
Dim strTest As String
strTest = Str$(123) ' Returns " 123".
strTest = Str$(-123.45) ' Returns "-123.45".
strTest = Str$(789.001) ' Returns " 789.001".