Type Conversion Functions (Visual Basic)
These functions are compiled inline, meaning the conversion code is part of the code that evaluates the expression. Sometimes there is no call to a procedure to accomplish the conversion, which improves performance. Each function coerces an expression to a specific data type.
CBool(expression) CByte(expression) CChar(expression) CDate(expression) CDbl(expression) CDec(expression) CInt(expression) CLng(expression) CObj(expression) CSByte(expression) CShort(expression) CSng(expression) CStr(expression) CUInt(expression) CULng(expression) CUShort(expression)
Required. Any expression of the source data type.
The function name determines the data type of the value it returns, as shown in the following table.
Return data type
Range for expression argument
Any valid Char or String or numeric expression.
0 through 255 (unsigned); fractional parts are rounded.1
Any valid Char or String expression; only first character of a String is converted; value can be 0 through 65535 (unsigned).
Any valid representation of a date and time.
-1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values.
+/-79,228,162,514,264,337,593,543,950,335 for zero-scaled numbers, that is, numbers with no decimal places. For numbers with 28 decimal places, the range is +/-7.9228162514264337593543950335. The smallest possible non-zero number is 0.0000000000000000000000000001 (+/-1E-28).
-2,147,483,648 through 2,147,483,647; fractional parts are rounded.1
-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807; fractional parts are rounded.1
Any valid expression.
-128 through 127; fractional parts are rounded.1
-32,768 through 32,767; fractional parts are rounded.1
-3.402823E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.402823E+38 for positive values.
Returns for CStr depend on the expression argument. See Return Values for the CStr Function (Visual Basic).
0 through 4,294,967,295 (unsigned); fractional parts are rounded.1
0 through 18,446,744,073,709,551,615 (unsigned); fractional parts are rounded.1
0 through 65,535 (unsigned); fractional parts are rounded.1
1 Fractional parts can be subject to a special type of rounding called banker's rounding. See "Remarks" for more information.
As a rule, you should use the Visual Basic type conversion functions in preference to the .NET Framework methods such as ToString(), either on the Convert class or on an individual type structure or class. The Visual Basic functions are designed for optimal interaction with Visual Basic code, and they also make your source code shorter and easier to read. In addition, the .NET Framework conversion methods do not always produce the same results as the Visual Basic functions, for example when converting Boolean to Integer. For more information, see Troubleshooting Data Types (Visual Basic).
Coercion. In general, you can use the data type conversion functions to coerce the result of an operation to a particular data type rather than the default data type. For example, use CDec to force decimal arithmetic in cases where single-precision, double-precision, or integer arithmetic would normally take place.
Failed Conversions. If the expression passed to the function is outside the range of the data type to which it is to be converted, an OverflowException occurs.
Fractional Parts. When you convert a nonintegral value to an integral type, the integer conversion functions (CByte, CInt, CLng, CSByte, CShort, CUInt, CULng, and CUShort) remove the fractional part and round the value to the closest integer.
If the fractional part is exactly 0.5, the integer conversion functions round it to the nearest even integer. For example, 0.5 rounds to 0, and 1.5 and 2.5 both round to 2. This is sometimes called banker's rounding, and its purpose is to compensate for a bias that could accumulate when adding many such numbers together.
Date/Time Conversions. Use the IsDate function to determine if a value can be converted to a date and time. CDate recognizes date literals and time literals but not numeric values. To convert a Visual Basic 6.0 Date value to a Date value in Visual Basic 2005 or later versions, you can use the DateTime.FromOADate method.
Neutral Date/Time Values. The Date Data Type (Visual Basic) always contains both date and time information. For purposes of type conversion, Visual Basic considers 1/1/0001 (January 1 of the year 1) to be a neutral value for the date, and 00:00:00 (midnight) to be a neutral value for the time. If you convert a Date value to a string, CStr does not include neutral values in the resulting string. For example, if you convert #January 1, 0001 9:30:00# to a string, the result is "9:30:00 AM"; the date information is suppressed. However, the date information is still present in the original Date value and can be recovered with functions such as DatePart function.
Culture Sensitivity. The type conversion functions involving strings perform conversions based on the current culture settings for the application. For example, CDate recognizes date formats according to the locale setting of your system. You must provide the day, month, and year in the correct order for your locale, or the date might not be interpreted correctly. A long date format is not recognized if it contains a day-of-the-week string, such as "Wednesday".
If you need to convert to or from a string representation of a value in a format other than the one specified by your locale, you cannot use the Visual Basic type conversion functions. To do this, use the ToString(IFormatProvider) and Parse(String, IFormatProvider) methods of that value's type. For example, use Double.Parse when converting a string to a Double, and use Double.ToString when converting a value of type Double to a string.
The CType Function takes a second argument, typename, and coerces expression to typename, where typename can be any data type, structure, class, or interface to which there exists a valid conversion.
The following example uses the CBool function to convert expressions to Boolean values. If an expression evaluates to a nonzero value, CBool returns True; otherwise, it returns False.
The following example uses the CChar function to convert the first character of a String expression to a Char type.
Dim aString As String Dim aChar As Char ' CChar converts only the first character of the string. aString = "BCD" ' The following line of code sets aChar to "B". aChar = CChar(aString)
The input argument to CChar must be of data type Char or String. You cannot use CChar to convert a number to a character, because CChar cannot accept a numeric data type. The following example obtains a number representing a code point (character code) and converts it to the corresponding character. It uses the InputBox function to obtain the string of digits, CInt to convert the string to type Integer, and ChrW to convert the number to type Char.
The following example uses the CDate function to convert strings to Date values. In general, hard-coding dates and times as strings (as shown in this example) is not recommended. Use date literals and time literals, such as #Feb 12, 1969# and #4:45:23 PM#, instead.
The following example uses the CLng function to convert values to Long.
The following example uses the CObj function to convert a numeric value to Object. The Object variable itself contains only a four-byte pointer, which points to the Double value assigned to it.
The following example uses the CSng function to convert values to Single.
The following example uses the CStr function to convert a numeric value to String.
Dim aDouble As Double Dim aString As String aDouble = 437.324 ' The following line of code sets aString to "437.324". aString = CStr(aDouble)
The following example uses the CStr function to convert Date values to String values.
Dim aDate As Date Dim aString As String ' The following line of code generates a COMPILER ERROR because of invalid format. ' aDate = #February 12, 1969 00:00:00# ' Date literals must be in the format #m/d/yyyy# or they are invalid. ' The following line of code sets the time component of aDate to midnight. aDate = #2/12/1969# ' The following conversion suppresses the neutral time value of 00:00:00. ' The following line of code sets aString to "2/12/1969". aString = CStr(aDate) ' The following line of code sets the time component of aDate to one second past midnight. aDate = #2/12/1969 12:00:01 AM# ' The time component becomes part of the converted value. ' The following line of code sets aString to "2/12/1969 12:00:01 AM". aString = CStr(aDate)
CStr always renders a Date value in the standard short format for the current locale, for example, "6/15/2003 4:35:47 PM". However, CStr suppresses the neutral values of 1/1/0001 for the date and 00:00:00 for the time.
For more detail on the values returned by CStr, see Return Values for the CStr Function (Visual Basic).