Call Int64TryParse(String, NumberStyles, IFormatProvider, Int32) : Data Type Convert « Data Type « VB.Net Tutorial






Imports System.Globalization

Module StringParsing
   Public Sub Main()
      Dim numericString As String
      Dim styles As NumberStyles

      numericString = "106779"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)

      numericString = "-30677"
      styles = NumberStyles.None
      CallTryParse(numericString, styles)

      styles = NumberStyles.AllowLeadingSign
      CallTryParse(numericString, styles)

      numericString = "301677-"
      CallTryParse(numericString, styles)

      styles = styles Or NumberStyles.AllowTrailingSign
      CallTryParse(numericString, styles)

      numericString = "$10634"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)

      styles = NumberStyles.Integer Or NumberStyles.AllowCurrencySymbol
      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 Long
      Dim provider As CultureInfo
      If CBool(styles And NumberStyles.AllowCurrencySymbol) Then
         provider = CultureInfo.CurrentCulture
      Else
         provider = New CultureInfo("en-US")
      End If

      Dim result As Boolean = Int64.TryParse(stringToConvert, styles,provider, 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








2.45.Data Type Convert
2.45.1.CLng: convert integer to long
2.45.2.CByte: convert to byte
2.45.3.CSng: convert to Single
2.45.4.CDbl: convert to double
2.45.5.CDec: convert to decimal
2.45.6.Call Int64TryParse(String, NumberStyles, IFormatProvider, Int32)
2.45.7.Convert a string into a 64-bit signed integer value using the Int64.Parse(String) method
2.45.8.Convert.ToBase64CharArray() and Convert.FromBase64CharArray methods
2.45.9.Converts the bit patterns of UInt32 values to Byte arrays with the GetBytes method.
2.45.10.Convert.ToString( non-numeric types, IFormatProvider ).
2.45.11.Create a hexadecimal value out of range of the UInt64 type and Convert it back to a number.