當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Int16.TryParse方法代碼示例

本文整理匯總了VB.NET中System.Int16.TryParse方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Int16.TryParse方法的具體用法?VB.NET Int16.TryParse怎麽用?VB.NET Int16.TryParse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。


在下文中一共展示了Int16.TryParse方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: StringParsing

Module StringParsing
   Public Sub Main()
      TryToParse(Nothing)
      TryToParse("16051")
      TryToParse("9432.0")
      TryToParse("16,667")
      TryToParse("   -322   ")
      TryToParse("+4302")
      TryToParse("(100)")
      TryToParse("01FA")
      
   End Sub
   
   Private Sub TryToParse(value As String)
      Dim number As Int16
      Dim result As Boolean = Int16.TryParse(value, number)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}.", value, number)
      Else
         If value Is Nothing Then value = "" 
         Console.WriteLine("Attempted conversion of '{0}' failed.", value)
      End If     
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:24,代碼來源:Int16.TryParse

輸出:

Attempted conversion of '' failed.
Converted '16051' to 16051.
Attempted conversion of '9432.0' failed.
Attempted conversion of '16,667' failed.
Converted '   -322   ' to -322.
Converted '+4302' to 4302.
Attempted conversion of '(100)' failed.
Attempted conversion of '01FA' failed.

示例2: StringParsing

' 導入命名空間
Imports System.Globalization

Module StringParsing
   Public Sub Main()
      Dim numericString As String
      Dim styles As NumberStyles
      
      numericString = "10677"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)
      
      numericString = "-30677"
      styles = NumberStyles.None
      CallTryParse(numericString, styles)
      
      numericString = "10345.00"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)
      
      numericString = "10345.72"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)

      numericString = "22,593" 
      styles = NumberStyles.Integer Or NumberStyles.AllowThousands
      CallTryParse(numericString, styles)
      
      numericString = "12E-01"
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(numericString, styles) 
          
      numericString = "12E03"
      CallTryParse(numericString, styles) 
      
      numericString = "80c1"
      CallTryParse(numericString, NumberStyles.HexNumber)
      
      numericString = "0x80C1"
      CallTryParse(numericString, NumberStyles.HexNumber)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
      Dim number As Short
      Dim result As Boolean = Int16.TryParse(stringToConvert, styles, _
                                             CultureInfo.InvariantCulture, number)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
      Else
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           Convert.ToString(stringToConvert))
      End If                                                                           
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:54,代碼來源:Int16.TryParse

輸出:

Converted '10677' to 10677.
Attempted conversion of '-30677' failed.
Converted '10345.00' to 10345.
Attempted conversion of '10345.72' failed.
Converted '22,593' to 22593.
Attempted conversion of '12E-01' failed.
Converted '12E03' to 12000.
Converted '80c1' to -32575.
Attempted conversion of '0x80C1' failed.


注:本文中的System.Int16.TryParse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。