本文整理汇总了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;未经允许,请勿转载。 |
请发表评论