Type Conversion Functions

 

If you code "mixed mode" assignment statements (i.e., assign a string to an integer, assign a single to a string, etc.), VB.NET will perform the necessary conversions in such statements whenever it possibly can (provided that "Option Strict" is off). Such conversions are called implicit conversions.

 

However, 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,  CDate, CDbl, CDec,  CInt, CLng, CSng, CStr, etc. Alternatively, you can use a corresponding method from the VB.NET Convert class to accomplish the same result.

 

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.

 

The "Cxxx" functions and their equivalent Convert class methods are shown in the table below:

 

Function

Convert Class Method

Description

CBool

Convert.ToBoolean

Converts a numeric or string expression to Boolean. Numeric 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 ***

 

blnTest = Convert.ToBoolean(50)     ' blnTest = True

blnTest = Convert.ToBoolean(0)      ' blnTest = False

blnTest = Convert.ToBoolean("-26")   ' blnTest = True

blnTest = Convert.ToBoolean("True") ' blnTest = True

blnTest = Convert.ToBoolean("False")' blnTest = False

blnTest = Convert.ToBoolean("ABC")  ' *** Error ***

 

CByte

Convert.ToByte

Converts a numeric expression in the range 0 to 255 to Byte. Fractional parts are rounded.

 

Examples:

 

bytX = CByte(125.5678) ' bytX = 126

bytX = CByte(300.99)   ' error

 

bytX = Convert.ToByte(125.5678) ' bytX = 126

bytX = Convert.ToByte(300.99)   ' error

 

CChar

Convert.ToChar

Converts a Char or String expression to Char data type. If String, only first character is converted.

 

CDate

Convert.ToDate

Converts a string expression containing a valid representation of a date/time to the Date (DateTime) data type. If the expression contains only a date portion, the resulting time component of the converted datetime value will be midnight. If the expression contains only a time portion, the resulting date component of the converted datetime value will be 1/1/0001.

 

Examples:

dtmTest = CDate("1/18/2012")                ' dtmTest = 1/18/2012 at midnight

dtmTest = CDate("13:22")                    ' dtmTest = 1/1/0001 at 1:22 PM

dtmTest = CDate("1/18/2021 1:22:00 PM")     ' dtmTest = 1/18/2012 at 1:22 PM

dtmTest = CDate("ABC")                      ' error

 

dtmTest = Convert.ToDate("1/18/2012")                ' dtmTest = 1/18/2012 at midnight

dtmTest = Convert.ToDate("13:22")                    ' dtmTest = 1/1/0001 at 1:22 PM

dtmTest = Convert.ToDate("1/18/2021 1:22:00 PM")     ' dtmTest = 1/18/2012 at 1:22 PM

dtmTest = Convert.ToDate("ABC")                     ' error     

 

CDbl

Convert.ToDouble

Converts a numeric or string expression to Double. If string, must be string representation of a number. The value of the expression must be within the Double range.

 

Examples:

dblTest = CDbl("1058.930") 'dblTest = 1058.93

dblTest = Convert.ToDouble("1058.930") 'dblTest = 1058.93

 

CDec

Convert.ToDecimal

Converts a numeric or string expression to Decimal. If string, must be string representation of a number. The value of the expression must be within the Decimal range.

 

Examples:

decTest = CDec("1058.930")              'decTest = 1058.93

decTest = Convert.ToDecimal("1058.930") 'decTest = 1058.93

 

CInt

Convert.ToInt32

Converts a numeric or string expression to Integer (Int32). If string, must be string representation of a number. The value of the expression must be within the Integer (Int32) range. Result is rounded.

 

Examples:

           

intX = CInt(-1.2) 'intX = -1

intX = CInt(-1.9) 'intX = -2

intX = CInt(3.69) 'intX = 4

intX = CInt(3.3)  'intX = 3

 

intX = Convert.ToInt32(-1.2)  'intX = -1

intX = Convert.ToInt32(-1.9)  'intX = -2

intX = Convert.ToInt32(3.69)  'intX = 4

intX = Convert.ToInt32(3.3)   'intX = 3

 

CLng

Convert.ToInt64

Converts a numeric or string expression to Long (Int64). If string, must be string representation of a number. The value of the expression must be within the Long (Int64) range. Result is rounded.

 

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

 

lngX = Convert.ToLng64(-1.2)  'lngX = -1

lngX = Convert.ToLng64(-1.9)  'lngX = -2

lngX = Convert.ToLng64(3.69)  'lngX = 4

lngX = Convert.ToLng64 (3.3)  'lngX = 3

 

CObj

N/A

Converts an expression of any type to an object. The object variable itself contains a four-byte pointer to expression assigned to it.

 

Example:

 

Dim intTest As Integer

Dim objTest As Object

' the following line sets objTest to a pointer to intTest

objTest = CObj(intTest)

 

CSByte

Convert.ToSByte

Converts a numeric expression in the range -128 to 127 to Signed Byte. Fractional parts are rounded.

 

CShort

Convert.ToInt16

Converts a numeric or string expression to Short (Int16). If string, must be string representation of a number. The value of the expression must be within the Short (Int16) range. Result is rounded.

 

Examples:

           

intX = CShort(-1.2)     'intX = -1

intX = CShort(-1.9)     'intX = -2

intX = CShort(3.69)     'intX = 4

intX = CShort(3.3)      'intX = 3

 

intX = Convert.ToInt16(-1.2)  'intX = -1

intX = Convert.ToInt16(-1.9)  'intX = -2

intX = Convert.ToInt16(3.69)  'intX = 4

intX = Convert.ToInt16 (3.3)  'intX = 3

 

CSng

Convert.ToSingle

Converts a numeric or string expression to Single. If string, must be string representation of a number. The value of the expression must be within the Single range.

 

Examples:

sngTest = CSng("1058.930") 'sngTest = 1058.93

sngTest = Convert.ToSingle("1058.930") 'sngTest = 1058.93

 

CStr

Convert.ToString

Converts a variable of any datatype to a string. If the argument is a Date, a "short date" string 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:

           

dtmTest1 = #1/18/2012#

dtmTest2 = #1/18/2012 1:00 PM#

blnTest = False

sngTest = 123.456

 

strTest = CStr(dtmTest1) 'strTest = "1/18/2012"

strTest = CStr(dtmTest2) 'strTest = "1/18/2012 1:00:00 PM"

strTest = CStr(blnTest)  'strTest = "False"

strTest = CStr(sngTest)  'strTest = "123.456"

 

strTest = Convert.ToString(dtmTest1) 'strTest = "1/18/2012"

strTest = Convert.ToString(dtmTest2) 'strTest = "1/18/2012 1:00:00 PM"

strTest = Convert.ToString(blnTest)  'strTest = "False"

strTest = Convert.ToString(sngTest)  'strTest = "123.456"

 

NOTE: "ToString" is also a method that can be applied to any data type. So the following statements are also valid:

 

strTest = dtmTest1.ToString 'strTest = "1/18/2012"

strTest = dtmTest2.ToString 'strTest = "1/18/2012 1:00:00 PM"

strTest = blnTest.ToString  'strTest = "False"

strTest = sngTest.ToString  'strTest = "123.456"

 

CUInt

Convert.ToUInt32

Converts a numeric or string expression to an unsigned integer (Int32). If string, must be string representation of a number. The value of the expression must be within the unsigned integer (Int32) range.

 

CULng

Convert.ToUInt64

Converts a numeric or string expression to an unsigned long integer (Int64). If string, must be string representation of a number. The value of the expression must be within the unsigned long integer (Int64) range.

 

CUShort

Convert.ToUInt16

Converts a numeric or string expression to an unsigned short integer (Int16). If string, must be string representation of a number. The value of the expression must be within the unsigned short integer (Int16) range.

 

 

The CType Function

The CType function converts an expression to a specified data type. The syntax is:

 

variable = CType(expression, typename)

 

where expression is any valid expression that is within the range permitted by typename. Typename is any valid type that would be permitted within an As clause in a Dim statement, such as Integer, Date, String, etc.

 

Example:

Dim strNum As String = "1234.56"

Dim decNum As Decimal

decNum = CType(strNum, Decimal)  ' decNum now contains 1234.56

 

 

The Val Function

 

The Val function is a generic 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 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".