本文整理匯總了VB.NET中System.SByte.Parse方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET SByte.Parse方法的具體用法?VB.NET SByte.Parse怎麽用?VB.NET SByte.Parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了SByte.Parse方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: modMain
' 導入命名空間
Imports System.Globalization
Module modMain
Public Sub Main()
Dim byteString As String
byteString = " 123"
ParseString(byteString, NumberStyles.None)
ParseString(byteString, NumberStyles.Integer)
byteString = "3A"
ParseString(byteString, NumberStyles.AllowHexSpecifier)
byteString = "21"
ParseString(byteString, NumberStyles.Integer)
ParseString(byteString, NumberStyles.AllowHexSpecifier)
byteString = "-22"
ParseString(byteString, NumberStyles.Integer)
ParseString(byteString, NumberStyles.AllowParentheses)
byteString = "(45)"
ParseString(byteString, NumberStyles.AllowParentheses)
byteString = "000,000,056"
ParseString(byteString, NumberStyles.Integer)
ParseString(byteString, NumberStyles.Integer Or NumberStyles.AllowThousands)
End Sub
Private Sub ParseString(value As String, style As NumberStyles)
Dim number As SByte
If value Is Nothing Then Console.WriteLine("Cannot parse a null string...")
Try
number = SByte.Parse(value, style, NumberFormatInfo.CurrentInfo)
Console.WriteLine("SByte.Parse('{0}', {1}) = {2}", value, style, number)
Catch e As FormatException
Console.WriteLine("'{0}' and {1} throw a FormatException", value, style)
Catch e As OverflowException
Console.WriteLine("'{0}' is outside the range of a signed byte",
value)
End Try
End Sub
End Module
' The example displays the following information to the console:
' ' 123' and None throw a FormatException
' SByte.Parse(" 123", Integer)) = 123
' SByte.Parse("3A", AllowHexSpecifier)) = 58
' SByte.Parse("21", Integer)) = 21
' SByte.Parse("21", AllowHexSpecifier)) = 33
' SByte.Parse("-22", Integer)) = -22
' '-22' and AllowParentheses throw a FormatException
' SByte.Parse("(45)", AllowParentheses)) = -45
' '000,000,056' and Integer throw a FormatException
' SByte.Parse("000,000,056", Integer, AllowThousands)) = 56
示例2: Example
' 導入命名空間
Imports System.Globalization
Module Example
Public Sub Main()
Dim style As NumberStyles
Dim number As SByte
' Parse value with no styles allowed.
Dim values1() As String = { " 121 ", "121", "-121" }
style = NumberStyles.None
Console.WriteLine("Styles: {0}", style.ToString())
For Each value As String In values1
Try
number = SByte.Parse(value, style)
Console.WriteLine(" Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine(" Unable to parse '{0}'.", value)
End Try
Next
Console.WriteLine()
' Parse value with trailing sign.
style = NumberStyles.Integer Or NumberStyles.AllowTrailingSign
Dim values2() As String = { " 103+", " 103 +", "+103", "(103)", " +103 " }
Console.WriteLine("Styles: {0}", style.ToString())
For Each value As String In values2
Try
number = SByte.Parse(value, style)
Console.WriteLine(" Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine(" Unable to parse '{0}'.", value)
Catch e As OverflowException
Console.WriteLine(" '{0}' is out of range of the SByte type.", value)
End Try
Next
Console.WriteLine()
End Sub
End Module
輸出:
Styles: None Unable to parse ' 121 '. Converted '121' to 121. Unable to parse '-121'. Styles: Integer, AllowTrailingSign Converted ' 103+' to 103. Converted ' 103 +' to 103. Converted '+103' to 103. Unable to parse '(103)'. Converted ' +103 ' to 103.
示例3: values
' Define an array of numeric strings.
Dim values() As String = { "-16", " -3", "+ 12", " +12 ", " 12 ", _
"+120", "(103)", "192", "-160" }
' Parse each string and display the result.
For Each value As String In values
Try
Console.WriteLine("Converted '{0}' to the SByte value {1}.", _
value, SByte.Parse(value))
Catch e As FormatException
Console.WriteLine("'{0}' cannot be parsed successfully by SByte type.", _
value)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the SByte type.", _
value)
End Try
Next
輸出:
Converted '-16' to the SByte value -16. Converted ' -3' to the SByte value -3. + 12' cannot be parsed successfully by SByte type. Converted ' +12 ' to the SByte value 12. Converted ' 12 ' to the SByte value 12. Converted '+120' to the SByte value 120. (103)' cannot be parsed successfully by SByte type. 192' is out of range of the SByte type. -160' is out of range of the SByte type.
示例4: Example
' 導入命名空間
Imports System.Globalization
Module Example
Public Sub Main()
Dim nf As New NumberFormatInfo()
nf.NegativeSign = "~"
Dim values() As String = { "-103", "+12", "~16", " 1", "~255" }
Dim providers() As IFormatProvider = { nf, CultureInfo.InvariantCulture }
For Each provider As IFormatProvider In providers
Console.WriteLine("Conversions using {0}:", CObj(provider).GetType().Name)
For Each value As String In values
Try
Console.WriteLine(" Converted '{0}' to {1}.", _
value, SByte.Parse(value, provider))
Catch e As FormatException
Console.WriteLine(" Unable to parse '{0}'.", value)
Catch e As OverflowException
Console.WriteLine(" '{0}' is out of range of the SByte type.", value)
End Try
Next
Next
End Sub
End Module
' The example displays '
' Conversions using NumberFormatInfo:
' Unable to parse '-103'.
' Converted '+12' to 12.
' Converted '~16' to -16.
' Converted ' 1' to 1.
' '~255' is out of range of the SByte type.
' Conversions using CultureInfo:
' Converted '-103' to -103.
' Converted '+12' to 12.
' Unable to parse '~16'.
' Converted ' 1' to 1.
' Unable to parse '~255'.