类型转换函数 (Visual Basic)
Visual Studio 2015 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)
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) Dim aDouble As Double Dim aByte As Byte aDouble = 125.5678 ' The following line of code sets aByte to 126. aByte = CByte(aDouble) 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) 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) 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) 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) Dim aDouble As Double Dim aDecimal As Decimal aDouble = 10000000.0587 ' The following line of code sets aDecimal to 10000000.0587. aDecimal = CDec(aDouble) Dim aDbl As Double Dim anInt As Integer aDbl = 2345.5678 ' The following line of code sets anInt to 2346. anInt = CInt(aDbl) 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) 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) Dim aDouble As Double Dim anSByte As SByte aDouble = 39.501 ' The following line of code sets anSByte to 40. anSByte = CSByte(aDouble) Dim aByte As Byte Dim aShort As Short aByte = 100 ' The following line of code sets aShort to 100. aShort = CShort(aByte) 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) Dim aDouble As Double Dim aString As String aDouble = 437.324 ' The following line of code sets aString to "437.324". aString = CStr(aDouble) 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) Dim aDouble As Double Dim aUInteger As UInteger aDouble = 39.501 ' The following line of code sets aUInteger to 40. aUInteger = CUInt(aDouble) Dim aDouble As Double Dim aULong As ULong aDouble = 39.501 ' The following line of code sets aULong to 40. aULong = CULng(aDouble) Dim aDouble As Double Dim aUShort As UShort aDouble = 39.501 ' The following line of code sets aUShort to 40. aUShort = CUShort(aDouble) |
|
来自: maotuitui > 《Visual Basic》