这些函数是内联编译的,这意味着转换代码是计算表达式的代码的一部分。 有时,无需调用过程来完成转换,从而提高性能。 每个函数将表达式强制转换为特定数据类型。
语法
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)
部件
expression
必填。 源数据类型的任何表达式。
返回值数据类型
函数名称确定它返回的值的数据类型,如下表所示。
函数名称 | 返回数据类型 |
expression 参数的范围 |
---|---|---|
CBool |
布尔数据类型 | 任何有效 Char 或 String 数值表达式。 |
CByte |
字节数据类型 |
Byte.MinValue (0) 到 Byte.MaxValue (255) (无符号): 小数部分舍入。1 从 Visual Basic 15.8 开始,Visual Basic 使用 CByte 函数优化浮点到字节转换的性能;有关详细信息,请参阅 “备注 ”部分。 有关示例,请参阅 CInt 示例 部分。 |
CChar |
Char 数据类型 | 任何有效 Char 或 String 表达式;仅转换 a 的第一 String 个字符;值可以是 0 到 65535(无符号)。 |
CDate |
日期数据类型 | 日期和时间的任何有效表示形式。 |
CDbl |
Double 数据类型 | -1.79769313486231570E+308 到 -4.94065645841246544E-324(对于负值);正值的 4.94065645841246544E-324 到 1.79769313486231570E+308。 |
CDec |
十进制数据类型 | +/-79,228,162,514,264,337,593,543,950,335,零刻度数字,即没有小数位数的数字。 对于小数位数为 28 位的数字,范围为 +/-7.9228162514264337593543950335。 最小可能的非零数为 0.0000000000000000000000000000000001 (+/-1E-28)。 |
CInt |
整数数据类型 |
Int32.MinValue (-2,147,483,648)到 Int32.MaxValue (2,147,483,647);分数部分舍入。1 从 Visual Basic 15.8 开始,Visual Basic 使用 CInt 函数优化浮点到整数转换的性能;有关详细信息,请参阅 “备注 ”部分。 有关示例,请参阅 CInt 示例 部分。 |
CLng |
长数据类型 |
Int64.MinValue (-9,223,372,036,854,775,808)到 Int64.MaxValue (9,223,372,036,854,775,807);小数部分舍入。1 从 Visual Basic 15.8 开始,Visual Basic 使用函数优化浮点到 64 位整数转换 CLng 的性能;有关详细信息,请参阅 “备注 ”部分。 有关示例,请参阅 CInt 示例 部分。 |
CObj |
对象数据类型 | 任何有效的表达式。 |
CSByte |
SByte 数据类型 |
SByte.MinValue (-128)至 SByte.MaxValue (127):小数部分舍入。1 从 Visual Basic 15.8 开始,Visual Basic 使用函数优化浮点到已签名字节转换 CSByte 的性能;有关详细信息,请参阅 “备注 ”部分。 有关示例,请参阅 CInt 示例 部分。 |
CShort |
短数据类型 |
Int16.MinValue (-32,768)至 Int16.MaxValue (32,767):小数部分舍入。1 从 Visual Basic 15.8 开始,Visual Basic 使用 CShort 函数优化浮点到 16 位整数转换的性能;有关详细信息,请参阅 “备注 ”部分。 有关示例,请参阅 CInt 示例 部分。 |
CSng |
单个数据类型 | -3.402823E+38 到 -1.401298E-45(对于负值);正值的 1.401298E-45 到 3.402823E+38。 |
CStr |
字符串数据类型 | 返回 CStr 结果取决于 expression 参数。 请参阅 CStr 函数的返回值。 |
CUInt |
UInteger 数据类型 |
UInt32.MinValue (0) 到 UInt32.MaxValue (4,294,967,295) (未签名);分数部分舍入。1 从 Visual Basic 15.8 开始,Visual Basic 使用函数优化浮点到无符号整数转换 CUInt 的性能;有关详细信息,请参阅 “备注 ”部分。 有关示例,请参阅 CInt 示例 部分。 |
CULng |
ULong 数据类型 |
UInt64.MinValue (0) 到 UInt64.MaxValue (18,446,744,073,709,551,615) (未签名);分数部分舍入。1 从 Visual Basic 15.8 开始,Visual Basic 使用函数优化浮点到无符号长整数转换 CULng 的性能;有关详细信息,请参阅 “备注 ”部分。 有关示例,请参阅 CInt 示例 部分。 |
CUShort |
UShort 数据类型 |
UInt16.MinValue (0) 到 UInt16.MaxValue (65,535) (未签名): 小数部分舍入。1 从 Visual Basic 15.8 开始,Visual Basic 使用函数优化浮点到无符号 16 位整数转换 CUShort 的性能;有关详细信息,请参阅 “备注 ”部分。 有关示例,请参阅 CInt 示例 部分。 |
1 小数部分可以接受一种特殊的舍入类型,称为 银行家舍入。 有关详细信息,请参阅“备注”。
注解
作为规则,应优先使用 Visual Basic 类型转换函数,例如 ToString()
,在 Convert 类或单个类型结构或类上使用 .NET Framework 方法。 Visual Basic 函数设计为与 Visual Basic 代码的最佳交互,并且还使源代码更短、更易于阅读。 此外,.NET Framework 转换方法并不总是生成与 Visual Basic 函数相同的结果,例如转换为 Boolean
Integer
。 有关详细信息,请参阅 “数据类型疑难解答”。
从 Visual Basic 15.8 开始,将以下方法返回的或值传递给Single一个整数转换函数(CByte
、CLng
CSByte
CInt
CUShort
CShort
、 CUInt
、CULng
) 时,优化浮点到整数转换的性能:Double
- Conversion.Fix(Double)
- Conversion.Fix(Object)
- Conversion.Fix(Single)
- Conversion.Int(Double)
- Conversion.Int(Object)
- Conversion.Int(Single)
- Math.Ceiling(Double)
- Math.Floor(Double)
- Math.Round(Double)
- Math.Truncate(Double)
此优化允许执行大量整数转换的代码运行速度高达两倍。 以下示例演示了这些优化的浮点到整数转换:
Dim s As Single = 173.7619
Dim d As Double = s
Dim i1 As Integer = CInt(Fix(s)) ' Result: 173
Dim b1 As Byte = CByte(Int(d)) ' Result: 173
Dim s1 AS Short = CShort(Math.Truncate(s)) ' Result: 173
Dim i2 As Integer = CInt(Math.Ceiling(d)) ' Result: 174
Dim i3 As Integer = CInt(Math.Round(s)) ' Result: 174
行为
强迫。 通常,可以使用数据类型转换函数将作的结果强制转换为特定数据类型,而不是默认数据类型。 例如,在单精度、双精度或整数算术通常发生的情况下,用于
CDec
强制进行十进制算术。转换失败。
expression
如果传递给函数的数据类型超出要转换到的数据类型范围,则会发生。OverflowException小数部分。 将非整型值转换为整型时,整数转换函数(
CByte
、、CInt
、CLng
、CShort
CSByte
、、CUInt
、和CULng
CUShort
)将小数部分除以最接近的整数。如果小数部分正好为 0.5,则整数转换函数将它舍入到最接近的偶数整数。 例如,0.5 舍入为 0,1.5 和 2.5 舍入为 2。 这有时被称为 银行家的舍入,其用途是补偿一个偏见,在一起添加许多此类数字时可能会累积。
CInt
与CLng
截断数字的小数部分(而不是舍入)的函数Fix不同Int。 此外,Fix
始终Int
返回与传入相同的数据类型的值。日期/时间转换。 使用该 IsDate 函数确定值是否可以转换为日期和时间。
CDate
识别日期文本和时间文本,但不能识别数值。 若要将 Visual Basic 6.0Date
值转换为Date
Visual Basic 2005 或更高版本中的值,可以使用该方法 DateTime.FromOADate 。中性日期/时间值。 日期数据类型始终包含日期和时间信息。 出于类型转换的目的,Visual Basic 将 1/1/0001(第 1 年 1 月 1 日)视为日期的 中性值 ,而 00:00:00(午夜)是时间的中性值。 如果将值转换为
Date
字符串,CStr
则不会在生成的字符串中包含中性值。 例如,如果转换为#January 1, 0001 9:30:00#
字符串,则结果为“上午 9:30:00”;日期信息将被禁止显示。 但是,日期信息仍存在于原始Date
值中,可以使用函数(如函数) DatePart 进行恢复。区域性敏感度。 涉及字符串的类型转换函数根据应用程序的当前区域性设置执行转换。 例如,
CDate
根据系统的区域设置识别日期格式。 必须按区域设置的正确顺序提供日期、月份和年份,否则日期可能无法正确解释。 如果长日期格式包含星期几字符串(如“Wednesday”),则无法识别长日期格式。如果需要以区域设置指定的格式或从值字符串表示形式转换为或从字符串表示形式,则不能使用 Visual Basic 类型转换函数。 为此,请使用
ToString(IFormatProvider)
该值的类型和Parse(String, IFormatProvider)
方法。 例如,在将字符串转换为字符串时使用Double.Parse,并在将类型为Double
字符串的值转换为字符串时使用Double.ToString。Double
CType 函数
CType 函数采用第二个参数typename
,并强制expression
typename
执行,其中typename
可以是存在有效转换的任何数据类型、结构、类或接口。
将 CType
与其他类型转换关键字进行比较,请参阅 DirectCast 运算符 和 TryCast 运算符。
CBool 示例
以下示例使用 CBool
函数将表达式转换为 Boolean
值。 如果表达式的计算结果为非零值, CBool
则 True
返回;否则返回 False
。
Dim a, b, c As Integer
Dim check As Boolean
a = 5
b = 5
' The following line of code sets check to True.
check = CBool(a = b)
c = 0
' The following line of code sets check to False.
check = CBool(c)
CByte 示例
以下示例使用 CByte
函数将表达式转换为 Byte
.
Dim aDouble As Double
Dim aByte As Byte
aDouble = 125.5678
' The following line of code sets aByte to 126.
aByte = CByte(aDouble)
CChar 示例
以下示例使用 CChar
函数将表达式的第一 String
个字符转换为类型 Char
。
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)
要 CChar
设置为数据类型 Char
的输入参数或 String
。 不能用于 CChar
将数字转换为字符,因为 CChar
不能接受数字数据类型。 下面的示例获取一个表示代码点(字符代码)的数字,并将其转换为相应的字符。 它使用 InputBox 函数获取数字字符串, CInt
将字符串转换为类型 Integer
,并将 ChrW
数字转换为类型 Char
。
Dim someDigits As String
Dim codePoint As Integer
Dim thisChar As Char
someDigits = InputBox("Enter code point of character:")
codePoint = CInt(someDigits)
' The following line of code sets thisChar to the Char value of codePoint.
thisChar = ChrW(codePoint)
CDate 示例
以下示例使用 CDate
函数将字符串转换为 Date
值。 通常,不建议将日期和时间硬编码为字符串(如此示例所示)。 请改用日期文本和时间文本,例如 #Feb 12、1969# 和 #4:45:23 PM#。
Dim aDateString, aTimeString As String
Dim aDate, aTime As Date
aDateString = "February 12, 1969"
aTimeString = "4:35:47 PM"
' The following line of code sets aDate to a Date value.
aDate = CDate(aDateString)
' The following line of code sets aTime to Date value.
aTime = CDate(aTimeString)
CDbl 示例
Dim aDec As Decimal
Dim aDbl As Double
' The following line of code uses the literal type character D to make aDec a Decimal.
aDec = 234.456784D
' The following line of code sets aDbl to 1.9225456288E+1.
aDbl = CDbl(aDec * 8.2D * 0.01D)
CDec 示例
以下示例使用 CDec
函数将数值 Decimal
转换为 。
Dim aDouble As Double
Dim aDecimal As Decimal
aDouble = 10000000.0587
' The following line of code sets aDecimal to 10000000.0587.
aDecimal = CDec(aDouble)
CInt 示例
下面的示例使用 CInt
函数将值 Integer
转换为 。
Dim aDbl As Double
Dim anInt As Integer
aDbl = 2345.5678
' The following line of code sets anInt to 2346.
anInt = CInt(aDbl)
CLng 示例
以下示例使用 CLng
函数将值 Long
转换为 。
Dim aDbl1, aDbl2 As Double
Dim aLng1, aLng2 As Long
aDbl1 = 25427.45
aDbl2 = 25427.55
' The following line of code sets aLng1 to 25427.
aLng1 = CLng(aDbl1)
' The following line of code sets aLng2 to 25428.
aLng2 = CLng(aDbl2)
CObj 示例
以下示例使用 CObj
函数将数值 Object
转换为 。 变量 Object
本身仅包含一个四字节指针,该指针指向 Double
分配给它的值。
Dim aDouble As Double
Dim anObject As Object
aDouble = 2.7182818284
' The following line of code sets anObject to a pointer to aDouble.
anObject = CObj(aDouble)
CSByte 示例
以下示例使用 CSByte
函数将数值 SByte
转换为 。
Dim aDouble As Double
Dim anSByte As SByte
aDouble = 39.501
' The following line of code sets anSByte to 40.
anSByte = CSByte(aDouble)
CShort 示例
以下示例使用 CShort
函数将数值 Short
转换为 。
Dim aByte As Byte
Dim aShort As Short
aByte = 100
' The following line of code sets aShort to 100.
aShort = CShort(aByte)
CSng 示例
以下示例使用 CSng
函数将值 Single
转换为 。
Dim aDouble1, aDouble2 As Double
Dim aSingle1, aSingle2 As Single
aDouble1 = 75.3421105
aDouble2 = 75.3421567
' The following line of code sets aSingle1 to 75.34211.
aSingle1 = CSng(aDouble1)
' The following line of code sets aSingle2 to 75.34216.
aSingle2 = CSng(aDouble2)
CStr 示例
以下示例使用 CStr
函数将数值 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)
以下示例使用CStr
函数将值转换为Date
String
值。
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
始终以标准短格式呈现 Date
当前区域设置的值,例如“6/15/2003 4:35:47 PM”。 但是, CStr
取消日期的中 性值 1/1/0001,同时禁止显示时间的 00:00:00。
有关返回 CStr
的值的详细信息,请参阅 CStr 函数的返回值。
CUInt 示例
以下示例使用 CUInt
函数将数值 UInteger
转换为 。
Dim aDouble As Double
Dim aUInteger As UInteger
aDouble = 39.501
' The following line of code sets aUInteger to 40.
aUInteger = CUInt(aDouble)
CULng 示例
以下示例使用 CULng
函数将数值 ULong
转换为 。
Dim aDouble As Double
Dim aULong As ULong
aDouble = 39.501
' The following line of code sets aULong to 40.
aULong = CULng(aDouble)
CUShort 示例
以下示例使用 CUShort
函数将数值 UShort
转换为 。
Dim aDouble As Double
Dim aUShort As UShort
aDouble = 39.501
' The following line of code sets aUShort to 40.
aUShort = CUShort(aDouble)