本文整理匯總了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
輸出:
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
輸出:
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
輸出:
-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
輸出:
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.