Type Conversion Functions
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)
The function name determines the data type of the value it returns, as shown in the following table.
| Function name | Return data type | Range for expression argument |
|---|---|---|
| CBool | Any valid Char or String or numeric expression. | |
| CByte | 0 through 255 (unsigned); fractional parts are rounded.1 | |
| CChar | Any valid Char or String expression; only first character of a String is converted; value can be 0 through 65535 (unsigned). | |
| CDate | Any valid representation of a date and time. | |
| CDbl | -1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values. | |
| CDec | +/-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). | |
| CInt | -2,147,483,648 through 2,147,483,647; fractional parts are rounded.1 | |
| CLng | -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807; fractional parts are rounded.1 | |
| CObj | Any valid expression. | |
| CSByte | -128 through 127; fractional parts are rounded.1 | |
| CShort | -32,768 through 32,767; fractional parts are rounded.1 | |
| CSng | -3.402823E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.402823E+38 for positive values. | |
| CStr | Returns for CStr depend on the expression argument. See Return Values for the CStr Function. | |
| CUInt | 0 through 4,294,967,295 (unsigned); fractional parts are rounded.1 | |
| CULng | 0 through 18,446,744,073,709,551,615 (unsigned); fractional parts are rounded.1 | |
| CUShort | 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.
Behavior
-
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.
CInt and CLng differ from the Int, Fix Functions (Visual Basic), which truncate, rather than round, the fractional part of a number. Also, Fix and Int always return a value of the same data type as you pass in.
-
Date/Time Conversions. Use the IsDate Function (Visual Basic) 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 Visual Basic 2005 Date value, you can use the System.DateTime.FromOADate(System.Double) 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 (Visual Basic).
-
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 System.Double.Parse(System.String,System.IFormatProvider) when converting a string to a Double, and use System.Double.ToString(System.IFormatProvider) when converting a value of type Double to a string.
CType Function
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.
For a comparison of CType with the other type conversion keywords, see DirectCast and TryCast.
CBool Example
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.
CByte Example
The following example uses the CByte function to convert an expression to a Byte.
CChar Example
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 (Visual Basic) to obtain the string of digits, CInt to convert the string to type Integer, and ChrW to convert the number to type Char.
CDate Example
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.
CDbl Example
CDec Example
The following example uses the CDec function to convert a numeric value to Decimal.
CInt Example
The following example uses the CInt function to convert a value to Integer.
CLng Example
The following example uses the CLng function to convert values to Long.
CObj Example
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.
CSByte Example
The following example uses the CSByte function to convert a numeric value to SByte.
CShort Example
The following example uses the CShort function to convert a numeric value to Short.
CSng Example
The following example uses the CSng function to convert values to Single.
CStr Example
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.
CUInt Example
The following example uses the CUInt function to convert a numeric value to UInteger.
CULng Example
The following example uses the CULng function to convert a numeric value to ULong.