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


VB.NET Byte.TryParse方法代码示例

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


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

示例1: ByteConversion

Module ByteConversion
   Public Sub Main()
      Dim byteStrings() As String = { Nothing, String.Empty, "1024", 
                                    "100.1", "100", "+100", "-100",
                                    "000000000000000100", "00,100",
                                    "   20   ", "FF", "0x1F"}

      For Each byteString As String In byteStrings
        CallTryParse(byteString)
      Next
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String)  
      Dim byteValue As Byte
      Dim success As Boolean = Byte.TryParse(stringToConvert, byteValue)
      If success Then
         Console.WriteLine("Converted '{0}' to {1}", _
                        stringToConvert, byteValue)
      Else
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           stringToConvert)
      End If                        
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:24,代码来源:Byte.TryParse

输出:

Attempted conversion of '' failed.
Attempted conversion of '' failed.
Attempted conversion of '1024' failed.
Attempted conversion of '100.1' failed.
Converted '100' to 100
Converted '+100' to 100
Attempted conversion of '-100' failed.
Converted '000000000000000100' to 100
Attempted conversion of '00,100' failed.
Converted '   20   ' to 20
Attempted conversion of 'FF' failed.
Attempted conversion of '0x1F' failed.

示例2: ByteConversion2

' 导入命名空间
Imports System.Globalization

Module ByteConversion2
   Public Sub Main()
      Dim byteString As String 
      Dim styles As NumberStyles
      
      byteString = "1024"
      styles = NumberStyles.Integer
      CallTryParse(byteString, styles)
      
      byteString = "100.1"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(byteString, styles)
      
      byteString = "100.0"
      CallTryParse(byteString, styles)
      
      byteString = "+100"
      styles = NumberStyles.Integer Or NumberStyles.AllowLeadingSign _
               Or NumberStyles.AllowTrailingSign
      CallTryParse(byteString, styles)
      
      byteString = "-100"
      CallTryParse(byteString, styles)
      
      byteString = "000000000000000100"
      CallTryParse(byteString, styles)
      
      byteString = "00,100"      
      styles = NumberStyles.Integer Or NumberStyles.AllowThousands
      CallTryParse(byteString, styles)
      
      byteString = "2E+3   "
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(byteString, styles)
      
      byteString = "FF"
      styles = NumberStyles.HexNumber
      CallTryParse(byteString, styles)
      
      byteString = "0x1F"
      CallTryParse(byteString, styles)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles As NumberStyles)  
      Dim byteValue As Byte
      Dim result As Boolean = Byte.TryParse(stringToConvert, styles, Nothing, _
                                            byteValue)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}", _
                        stringToConvert, byteValue)
      Else
         If stringToConvert Is Nothing Then stringToConvert = ""
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           stringToConvert.ToString())
      End If                        
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:60,代码来源:Byte.TryParse

输出:

Attempted conversion of '1024' failed.
Attempted conversion of '100.1' failed.
Converted '100.0' to 100
Converted '+100' to 100
Attempted conversion of '-100' failed.
Converted '000000000000000100' to 100
Converted '00,100' to 100
Attempted conversion of '2E+3   ' failed.
Converted 'FF' to 255
Attempted conversion of '0x1F' failed.


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