本文整理汇总了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))
示例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
输出:
32767 --> 111111111111111 (base 2) --> 32767 32767 --> 77777 (base 8) --> 32767 32767 --> 32767 (base 10) --> 32767 32767 --> 7fff (base 16) --> 32767