当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET UInt16.Parse方法代码示例

本文整理汇总了VB.NET中System.UInt16.Parse方法的典型用法代码示例。如果您正苦于以下问题:VB.NET UInt16.Parse方法的具体用法?VB.NET UInt16.Parse怎么用?VB.NET UInt16.Parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。


在下文中一共展示了UInt16.Parse方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: Example

' 导入命名空间
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "fr-FR" }
      Dim styles() As NumberStyles = { NumberStyles.Integer, _
                                       NumberStyles.Integer Or NumberStyles.AllowDecimalPoint }
      Dim values() As String = { "1702", "+1702.0", "+1702,0", "-1032.00", _
                                 "-1032,00", "1045.1", "1045,1" }
      
      ' Parse strings using each culture
      For Each cultureName As String In cultureNames
         Dim ci As New CultureInfo(cultureName)
         Console.WriteLine("Parsing strings using the {0} culture", ci.DisplayName)
         ' Use each style.
         For Each style As NumberStyles In styles
            Console.WriteLine("   Style: {0}", style.ToString())
            ' Parse each numeric string.
            For Each value As String In values
               Try
                  Console.WriteLine("      Converted '{0}' to {1}.", value, _
                                    UInt16.Parse(value, style, ci))
               Catch e As FormatException
                  Console.WriteLine("      Unable to parse '{0}'.", value)   
               Catch e As OverflowException
                  Console.WriteLine("      '{0}' is out of range of the UInt16 type.", _
                                    value)         
               End Try
            Next
         Next
      Next                                    
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:34,代码来源:UInt16.Parse

输出:

Parsing strings using the English (United States) culture
Style: Integer
Converted '1702' to 1702.
Unable to parse '+1702.0'.
Unable to parse '+1702,0'.
Unable to parse '-1032.00'.
Unable to parse '-1032,00'.
Unable to parse '1045.1'.
Unable to parse '1045,1'.
Style: Integer, AllowDecimalPoint
Converted '1702' to 1702.
Converted '+1702.0' to 1702.
Unable to parse '+1702,0'.
-1032.00' is out of range of the UInt16 type.
Unable to parse '-1032,00'.
1045.1' is out of range of the UInt16 type.
Unable to parse '1045,1'.
Parsing strings using the French (France) culture
Style: Integer
Converted '1702' to 1702.
Unable to parse '+1702.0'.
Unable to parse '+1702,0'.
Unable to parse '-1032.00'.
Unable to parse '-1032,00'.
Unable to parse '1045.1'.
Unable to parse '1045,1'.
Style: Integer, AllowDecimalPoint
Converted '1702' to 1702.
Unable to parse '+1702.0'.
Converted '+1702,0' to 1702.
Unable to parse '-1032.00'.
-1032,00' is out of range of the UInt16 type.
Unable to parse '1045.1'.
1045,1' is out of range of the UInt16 type.

示例2: Example

' 导入命名空间
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim values() As String = { " 214 ", "1,064", "(0)", "1241+", " + 214 ", " +214 ", "2153.0", "1e03", "1300.0e-2" }
      Dim whitespace As NumberStyles =  NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite
      Dim styles() As NumberStyles = { NumberStyles.None, _
                                       whitespace, _
                                       NumberStyles.AllowLeadingSign Or NumberStyles.AllowTrailingSign Or whitespace, _
                                       NumberStyles.AllowThousands Or NumberStyles.AllowCurrencySymbol, _
                                       NumberStyles.AllowExponent Or NumberStyles.AllowDecimalPoint }

      ' Attempt to convert each number using each style combination.
      For Each value As String In values
         Console.WriteLine("Attempting to convert '{0}':", value)
         For Each style As NumberStyles In styles
            Try
               Dim number As UShort = UInt16.Parse(value, style)
               Console.WriteLine("   {0}: {1}", style, number)
            Catch e As FormatException
               Console.WriteLine("   {0}: Bad Format", style)
            End Try         
         Next
         Console.WriteLine()
      Next
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:28,代码来源:UInt16.Parse

输出:

Attempting to convert ' 214 ':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: 214
Integer, AllowTrailingSign: 214
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert '1,064':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: 1064
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert '(0)':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert '1241+':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: 1241
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert ' + 214 ':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert ' +214 ':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: 214
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert '2153.0':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: 2153

Attempting to convert '1e03':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: 1000

Attempting to convert '1300.0e-2':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: 13

示例3: Example

Module Example
   Public Sub Main()
      Dim values() As String = { "-0", "17", "-12", "185", "66012", _ 
                                 "+0", "", Nothing, "16.1", "28.0", _
                                 "1,034" }
      For Each value As String In values
         Try
            Dim number As UShort = UInt16.Parse(value)
            Console.WriteLine("'{0}' --> {1}", value, number)
         Catch e As FormatException
            Console.WriteLine("'{0}' --> Bad Format", value)
         Catch e As OverflowException   
            Console.WriteLine("'{0}' --> OverflowException", value)
         Catch e As ArgumentNullException
            Console.WriteLine("'{0}' --> Null", value)
         End Try
      Next                                 
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:19,代码来源:UInt16.Parse

输出:

-0' --> 0
17' --> 17
-12' --> OverflowException
185' --> 185
66012' --> OverflowException
+0' --> 0
--> Bad Format
--> Null
16.1' --> Bad Format
28.0' --> Bad Format
1,034' --> Bad Format

示例4: Example

' 导入命名空间
Imports System.Globalization

Module Example
   Public Sub Main()
      ' Define a custom culture that uses "++" as a positive sign. 
      Dim ci As CultureInfo = New CultureInfo("")
      ci.NumberFormat.PositiveSign = "++"
      ' Create an array of cultures.
      Dim cultures() As CultureInfo = { ci, CultureInfo.InvariantCulture }
      ' Create an array of strings to parse.
      Dim values() As String = { "++1403", "-0", "+0", "+16034", _
                                 Int16.MinValue.ToString(), "14.0", "18012" }
      ' Parse the strings using each culture.
      For Each culture As CultureInfo In cultures
         Console.WriteLine("Parsing with the '{0}' culture.", culture.Name)
         For Each value As String In values
            Try
               Dim number As UShort = UInt16.Parse(value, culture)
               Console.WriteLine("   Converted '{0}' to {1}.", value, number)
            Catch e As FormatException
               Console.WriteLine("   The format of '{0}' is invalid.", value)
            Catch e As OverflowException
               Console.WriteLine("   '{0}' is outside the range of a UInt16 value.", value)
            End Try               
         Next
      Next
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:29,代码来源:UInt16.Parse

输出:

Parsing with the  culture.
Converted '++1403' to 1403.
Converted '-0' to 0.
The format of '+0' is invalid.
The format of '+16034' is invalid.
-32768' is outside the range of a UInt16 value.
The format of '14.0' is invalid.
Converted '18012' to 18012.
Parsing with the '' culture.
The format of '++1403' is invalid.
Converted '-0' to 0.
Converted '+0' to 0.
Converted '+16034' to 16034.
-32768' is outside the range of a UInt16 value.
The format of '14.0' is invalid.
Converted '18012' to 18012.


注:本文中的System.UInt16.Parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。