類型轉換函式 (Visual Basic)

這些函式會內嵌編譯,表示轉換程式碼是評估運算式之程式碼的一部分。 有時候不會呼叫程序來完成轉換,這可改善效能。 每個函式都會將運算式強制轉型為特定資料類型。

語法

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 Boolean 資料類型 任何有效的 CharString 或數值運算式。
CByte Byte 資料類型 Byte.MinValue (0) 到 Byte.MaxValue (255) (不帶正負號);小數部分會四捨五入。1

從 Visual Basic 15.8 開始,Visual Basic 會使用 CByte 函式最佳化浮點對位元組轉換的效能;如需詳細資訊,請參閱備註一節。 如需範例,請參閱 CInt 範例一節。
CChar Char 資料類型 任何有效 CharString 運算式;只會轉換 String 的第一個字元;值可為 0 到 65535 (不帶正負號)。
CDate Date 資料類型 日期和時間的任何有效表示。
CDbl Double 資料類型 負值為 -1.79769313486231570E+308 到 -4.94065645841246544E-324;正值為 4.94065645841246544E-324 到 1.79769313486231570E+308。
CDec Decimal 資料類型 歸零數字為 +/-79,228,162,514,264,337,593,543,950,335,也就是沒有小數位數的數字。 對於具有 28 個小數位數的數字,範圍是 +/-7.9228162514264337593543950335。 最小的可能非零數字為 0.0000000000000000000000000001 (+/-1E-28)。
CInt Integer 資料類型 Int32.MinValue (-2,147,483,648) 到 Int32.MaxValue (2,147,483,647);分數部分會四捨五入。1

從 Visual Basic 15.8 開始,Visual Basic 會使用 CInt 函式最佳化浮點對整數轉換的效能;如需詳細資訊,請參閱備註一節。 如需範例,請參閱 CInt 範例一節。
CLng Long 資料類型 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 會使用 CLng 函式最佳化浮點對 64 位元整數轉換的效能;如需詳細資訊,請參閱備註一節。 如需範例,請參閱 CInt 範例一節。
CObj Object Data Type 任何有效的運算式。
CSByte SByte 資料類型 SByte.MinValue (-128) 到 SByte.MaxValue (127);分數部分會四捨五入。1

從 Visual Basic 15.8 開始,Visual Basic 會使用 CSByte 函式最佳化浮點對帶正負號的位元組轉換的效能;如需詳細資訊,請參閱備註一節。 如需範例,請參閱 CInt 範例一節。
CShort Short 資料類型 Int16.MinValue (-32,768) 到 Int16.MaxValue (32,767);分數部分會四捨五入。1

從 Visual Basic 15.8 開始,Visual Basic 會使用 CShort 函式最佳化浮點對 16 位元整數轉換的效能;如需詳細資訊,請參閱備註一節。 如需範例,請參閱 CInt 範例一節。
CSng Single 資料類型 負值為 -3.402823E+38 到 -1.401298E-45;正值為 1.401298E-45 到 3.402823E+38。
CStr String 資料類型 傳回的 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 會使用 CUShort 函式最佳化浮點對不帶正負號的 16 位元整數轉換的效能;如需詳細資訊,請參閱備註一節。 如需範例,請參閱 CInt 範例一節。

1小數部分可能需遵守稱為「四捨六入五成雙」的特殊進位類型。 如需詳細資訊,請參閱「備註」。

備註

根據規則,您應該使用 Visual Basic 類型轉換函式,喜好設定為 .NET Framework 方法 (例如 ToString()),在 Convert 類別或在個別類型結構或類別上。 Visual Basic 函式是專為與 Visual Basic 程式碼進行最佳互動所設計,同時也可縮短您的原始程式碼且更容易閱讀。 此外,.NET Framework 轉換方法不一定會產生與 Visual Basic 函式相同的結果,例如,將 Boolean 轉換成 Integer 時。 如需詳細資訊,請參閱資料類型疑難排解

從 Visual Basic 15.8 開始,當您將下列方法傳回的 SingleDouble 值傳遞給其中一個整數轉換函式時,就會將浮點對整數轉換的效能最佳化 (CByteCShortCIntCLngCSByteCUShortCUIntCULng):

這項最佳化可讓執行大量整數轉換的程式碼執行速度最快提高為兩倍。 下列範例說明這些最佳化的浮點對整數轉換:

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

  • 小數部分。 當您將非整數值轉換成整數類型時,整數轉換函式 (CByteCIntCLngCSByteCShortCUIntCULngCUShort) 會移除小數部分,並將值四捨五入為最接近的整數。

    如果小數部分剛好是 0.5,整數轉換函式就會將其四捨五入為最接近的偶數整數。 例如,0.5 會四捨五入為 0,而 1.5 和 2.5 都會四捨五入為 2。 這有時稱為「四捨六入五成雙」,其用途是補償在將許多這類數字加在一起時,可能會累積的偏差。

    CIntCLngIntFix 函式不同,其會截斷 (而非四捨五入) 數字的小數部分。 此外,FixInt 也一律會傳回與您所傳入相同的資料類型值。

  • 日期/時間轉換。 使用 IsDate 函式來判斷值是否可轉換成日期和時間。 CDate 可辨識日期常值和時間常值,但無法辨識數值。 若要將 Visual Basic 6.0 Date 值轉換成 Visual Basic 2005 或更新版本中的 Date 值,您可以使用 DateTime.FromOADate 方法。

  • 中性日期/時間值。 Date 資料類型一律會包含日期和時間資訊。 為了進行類型轉換,Visual Basic 會將 1/1/0001 (1 年 1 月 1 日) 視為日期的「中性值」,並將 00:00:00 (午夜) 視為時間的中性值。 如果您將 Date 值轉換成字串,則 CStr 不會在產生的字串中包含中性值。 例如,如果您將 #January 1, 0001 9:30:00# 轉換成字串,結果會是「9:30:00 AM」;日期資訊隨即會隱藏。 不過,日期資訊仍存在於原始 Date 值中,且可以使用 DatePart 函式之類的函式來加以復原。

  • 文化特性敏感度。 涉及字串的類型轉換函式會根據應用程式的目前文化特性設定來執行轉換。 例如,CDate 會根據系統的地區設定來辨識日期格式。 您必須以正確的地區設定順序來提供日、月和年,否則可能無法正確解譯日期。 如果長日期格式包含星期幾字串 (例如「Wednesday」),則無法辨識該格式。

    如果您需要以您地區設定所指定值的格式來回轉換值的字串表示,則無法使用 Visual Basic 類型轉換函式。 若要這樣做,請使用該實值型別的 ToString(IFormatProvider)Parse(String, IFormatProvider) 方法。 例如,將字串轉換成 Double 時使用 Double.Parse,並在將 Double 型別的值轉換為字串時使用 Double.ToString

CType Function

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 的輸入引數必須是資料類型 CharString。 您無法使用 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 函式將數值轉換成 ObjectObject 變數本身僅包含四位元組指標,指向為其指派的 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)

另請參閱