當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Convert類代碼示例

本文整理匯總了VB.NET中System.Convert的典型用法代碼示例。如果您正苦於以下問題:VB.NET Convert類的具體用法?VB.NET Convert怎麽用?VB.NET Convert使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Convert類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1:

Dim dNumber As Double
dNumber = 23.15

Try
   ' Returns 23
   Dim iNumber As Integer
   iNumber = System.Convert.ToInt32(dNumber)
Catch exp As System.OverflowException
   System.Console.WriteLine("Overflow in double to int conversion.")
End Try

' Returns True
Dim bNumber As Boolean
bNumber = System.Convert.ToBoolean(dNumber)

' Returns "23.15"
Dim strNumber As String
strNumber = System.Convert.ToString(dNumber)

Try
   ' Returns '2'
   Dim chrNumber As Char
   chrNumber = System.Convert.ToChar(strNumber.Chars(1))
Catch exp As System.ArgumentNullException
   System.Console.WriteLine("String is null.")
Catch exp As System.FormatException
   System.Console.WriteLine("String length is greater than 1.")
End Try

' System.Console.ReadLine() returns a string and it
' must be converted.
Dim newInteger As Integer
newInteger = 0
Try
   System.Console.WriteLine("Enter an integer:")
   newInteger = System.Convert.ToInt32(System.Console.ReadLine())
Catch exp As System.ArgumentNullException
   System.Console.WriteLine("String is null.")
Catch exp As System.FormatException
   System.Console.WriteLine("String does not consist of an " + _
       "optional sign followed by a series of digits.")
Catch exp As System.OverflowException
   System.Console.WriteLine("Overflow in string to int conversion.")
End Try

System.Console.WriteLine("Your integer as a double is {0}", _
                         System.Convert.ToDouble(newInteger))
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:47,代碼來源:Convert

示例2: Example

Module Example
   Public Sub Main()
      Dim baseValues() As Integer = { 2, 8, 10, 16 }
      Dim value As Short = Int16.MaxValue
      For Each baseValue in baseValues
         Dim s As String = Convert.ToString(value, baseValue)
         Dim value2 As Short = Convert.ToInt16(s, baseValue)

         Console.WriteLine("{0} --> {1} (base {2}) --> {3}", 
                           value, s, baseValue, value2)
      Next
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:13,代碼來源:Convert

輸出:

32767 --> 111111111111111 (base 2) --> 32767
32767 --> 77777 (base 8) --> 32767
32767 --> 32767 (base 10) --> 32767
32767 --> 7fff (base 16) --> 32767


注:本文中的System.Convert類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。