型別轉換函式 (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 資料型別 (Visual Basic)

任何有效的 Char 或 String 或數值運算式。

CByte

Byte 資料型別 (Visual Basic)

0 至 255 (不帶正負號);小數部分會四捨五入。1

CChar

Char 資料型別 (Visual Basic)

任何有效的 Char 或 String 運算式,只會轉換 String 的第一個字元,值可從 0 到 65535 (不帶正負號)。

CDate

Date 資料型別 (Visual Basic)

日期和時間的任何有效表示

CDbl

Double 資料型別 (Visual Basic)

負值為 -1.79769313486231570E+308 至 -4.94065645841246544E-324,正值為 4.94065645841246544E-324 至 1.79769313486231570E+308。

CDec

Decimal 資料型別 (Visual Basic)

零個小數的數字為 +/-79,228,162,514,264,337,593,543,950,335,也就是沒有小數位數的數字。 具有 28 個小數位數的數字範圍為 +/-7.9228162514264337593543950335。 最小的可能非零值為 0.0000000000000000000000000001 (+/-1E-28)。

CInt

Integer 資料型別 (Visual Basic)

-2,147,483,648 至 2,147,483,647;小數部分會捨入。1

CLng

Long 資料型別 (Visual Basic)

-9,223,372,036,854,775,808 至 9,223,372,036,854,775,807;小數部分會捨入。1

CObj

Object 資料型別

任何有效的運算式。

CSByte

SByte 資料型別 (Visual Basic)

-128 至 127;小數部分會捨入。1

CShort

Short 資料型別 (Visual Basic)

-32,768 至 32,767;小數部分會捨入。1

CSng

字串資料型別 (Visual Basic)

負值為 -3.402823E+38 至 -1.401298E-45;正值為 1.401298E-45 至 3.402823E+38。

CStr

String 資料型別 (Visual Basic)

CStr 的傳回值取決於 expression 引數。 請參閱CStr 函式的傳回值 (Visual Basic)

CUInt

UInteger 資料型別

0 至 4,294,967,295 (不帶正負號);小數部分會四捨五入。1

CULng

ULong 資料型別 (Visual Basic)

0 至 18,446,744,073,709,551,615 (不帶正負號);小數部分會四捨五入。1

CUShort

UShort 資料型別 (Visual Basic)

0 至 65,535 (不帶正負號);小數部分會四捨五入。1

1 可以用特殊的捨去類型來處理小數部分,稱為「四捨六入五成雙」(Banker's Rounding)。 如需詳細資訊,請參閱備註。

備註

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

行為

  • 強制型轉(Coercion):一般來說,您可使用資料型別轉換函式來將某運算結果強制轉換為特定資料型別,而不是轉換為預設的資料型別。 例如,您可以在一般會發生單精度、雙精度或整數運算情況時,使用 CDec 來強制執行十進位運算。

  • 轉換失敗:如果傳遞給函式的 expression 在其轉換的資料型別範圍之外,就會發生 OverflowException

  • 小數部分:當您將非整數值轉換成整數型別時,整數轉換函式 (CByte、CInt、CLng、CSByte、CShort、CUInt、CULng 和 CUShort) 會移除小數部分並將值捨入成最接近的整數。

    當小數部分正好為 0.5 時,整數轉換函式會將該數字捨入至最接近的偶數整數。 例如,0.5 會捨入至 0,1.5 和 2.5 皆會捨入至 2。 這個動作有時稱為「四捨六入五成雙」(Banker's Rounding),其目的是補償當您一起加總此類數字時會累積的偏差。

    CInt 和 CLng 與 IntFix 函式不同,它會截斷 (而非捨入) 數字的小數部分。 同樣地,Fix 和 Int 也總是會將傳入的相同資料型別數值傳回。

  • 日期/時間轉換使用 IsDate 函式來判斷值是否可以轉換成日期和時間。 CDate 會辨識日期常值和時間常值,但不會辨識數值。 若要將 Visual Basic 6.0 的 Date 值轉換成 Visual Basic 2005 (含) 以後版本的 Date 值,可使用 DateTime.FromOADate 方法。

  • 中性日期/時間值Date 資料型別 (Visual Basic) 永遠同時包含日期和時間資訊。 為了方便進行型別轉換,Visual Basic 將 1/1/0001 (1 年 1 月 1 日) 視為日期的「中性值」(Neutral Value),將 00:00:00 (午夜) 視為時間的中性值。 如果您將 Date 值轉換為字串,CStr 不會在產生的字串中包含中性值。 例如,如果您將 #January 1, 0001 9:30:00# 轉換為字串,結果會是 "9:30:00 AM";日期資訊會隱藏。 但是,日期資訊仍然存在於原始的 Date 值中,而且可以使用類似 DatePart 函式的函式來加以復原。

  • 區分文化特性:型別轉換函式包含字串,該字串可根據應用程式的目前文化特性設定來執行轉換。 例如,CDate 依據您系統上的地區設定 (Locale) 來辨認日期的格式。 您必須依照地區設定的正確順序提供日期、月份和年份,否則可能無法正確解譯日期。 如果完整日期格式包含星期幾的字串,例如 "Wednesday",則會無法辨識它。

    如果您需要在值的字串表示法之間轉換,且使用的格式不是您地區設定所指定的格式,則無法使用 Visual Basic 型別轉換函式。 若要這麼做,請使用該實值型別的 ToString(IFormatProvider) 和 Parse(String, IFormatProvider) 方法。 例如,將字串轉換成 Double 時使用 Double.Parse,將型別 Double 的值轉換成字串時則使用 Double.ToString

CType 函式

CType 函式會利用第二個引數 typename,將 expression 強制型轉為 typename,其中的 typename 可為任何資料型別、結構、類別,或可有效轉換的介面。

如需 CType 與其他型別轉換關鍵字的比較,請參閱 DirectCast 運算子 (Visual Basic)TryCast 運算子 (Visual Basic)

CBool 範例

下列範例使用 CBool 函式來將運算式轉換為 Boolean 值。 如果運算式判定為非零值 (Nonzero),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 變數本身只包含一個 4 位元組的指標,它會指向指派給它的 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 函式的傳回值 (Visual Basic)

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)

請參閱

參考

轉換函式 (Visual Basic)

Asc

AscW

Chr

ChrW

Int

Fix

Format

Hex

Oct

Str

Val

其他資源

Visual Basic 中的型別轉換