Share via


Byte.TryParse Method (String, Byte%)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Tries to convert the string representation of a number to its Byte equivalent, and returns a value that indicates whether the conversion succeeded.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function TryParse ( _
    s As String, _
    <OutAttribute> ByRef result As Byte _
) As Boolean
public static bool TryParse(
    string s,
    out byte result
)

Parameters

  • s
    Type: System.String
    A string that contains a number to convert. The string is interpreted using the Integer style.
  • result
    Type: System.Byte%
    When this method returns, contains the Byte value equivalent to the number contained in s if the conversion succeeded, or zero if the conversion failed. This parameter is passed uninitialized.

Return Value

Type: System.Boolean
true if s was converted successfully; otherwise, false.

Remarks

The conversion fails and the method returns false if the s parameter is not in the correct format, if it is nulla null reference (Nothing in Visual Basic), or if it represents a number less than MinValue or greater than MaxValue.

The Byte.TryParse(String, Byte%) method is similar to the Byte.Parse(String) method, except that TryParse(String, Byte%) does not throw an exception if the conversion fails.

The s parameter should be the string representation of a number in the following form:

[ws][sign]digits[ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element

Description

ws

Optional white space.

sign

An optional positive sign, as specified by the NumberFormatInfo.PositiveSign property of the current culture.

digits

A sequence of decimal digits that range from 0 to 9.

The s parameter is interpreted using the Integer style. In addition to the byte value's decimal digits, only leading and trailing spaces together with a leading sign are allowed. (If the sign is present, it must be a positive sign or the method throws an OverflowException.) To explicitly define the style elements together with the culture-specific formatting information that can be present in s, use the Byte.Parse(String, NumberStyles, IFormatProvider) method.

The s parameter is parsed using the formatting information in a NumberFormatInfo object for the current culture. For more information, see NumberFormatInfo.CurrentInfo.

This overload of the Byte.TryParse(String, Byte%) method interprets all digits in the s parameter as decimal digits. To parse the string representation of a hexadecimal number, call the Byte.TryParse(String, NumberStyles, IFormatProvider, Byte%) overload.

Examples

The following example calls the TryParse(String, Byte%) method with a number of different string values.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim byteString As String = Nothing
      CallTryParse(outputBlock, byteString)

      byteString = String.Empty
      CallTryParse(outputBlock, byteString)

      byteString = "1024"
      CallTryParse(outputBlock, byteString)

      byteString = "100.1"
      CallTryParse(outputBlock, byteString)

      byteString = "100"
      CallTryParse(outputBlock, byteString)

      byteString = "+100"
      CallTryParse(outputBlock, byteString)

      byteString = "-100"
      CallTryParse(outputBlock, byteString)

      byteString = "000000000000000100"
      CallTryParse(outputBlock, byteString)

      byteString = "00,100"
      CallTryParse(outputBlock, byteString)

      byteString = "   20   "
      CallTryParse(outputBlock, byteString)

      byteString = "FF"
      CallTryParse(outputBlock, byteString)

      byteString = "0x1F"
      CallTryParse(outputBlock, byteString)
   End Sub

   Private Sub CallTryParse(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal stringToConvert As String)
      Dim byteValue As Byte
      Dim result As Boolean = Byte.TryParse(stringToConvert, byteValue)
      If result Then
         outputBlock.Text += String.Format("Converted '{0}' to {1}", _
                        stringToConvert, byteValue) + vbCrLf
      Else
         If stringToConvert Is Nothing Then stringToConvert = ""
         outputBlock.Text += String.Format("Attempted conversion of '{0}' failed.", _
                           stringToConvert.ToString()) + vbCrLf
      End If
   End Sub
End Module
' The example displays the following output:
'       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.
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string byteString = null;
      CallTryParse(outputBlock, byteString);

      byteString = String.Empty;
      CallTryParse(outputBlock, byteString);

      byteString = "1024";
      CallTryParse(outputBlock, byteString);

      byteString = "100.1";
      CallTryParse(outputBlock, byteString);

      byteString = "100";
      CallTryParse(outputBlock, byteString);

      byteString = "+100";
      CallTryParse(outputBlock, byteString);

      byteString = "-100";
      CallTryParse(outputBlock, byteString);

      byteString = "000000000000000100";
      CallTryParse(outputBlock, byteString);

      byteString = "00,100";
      CallTryParse(outputBlock, byteString);

      byteString = "   20   ";
      CallTryParse(outputBlock, byteString);

      byteString = "FF";
      CallTryParse(outputBlock, byteString);

      byteString = "0x1F";
      CallTryParse(outputBlock, byteString);
   }

   private static void CallTryParse(System.Windows.Controls.TextBlock outputBlock, string stringToConvert)
   {
      byte byteValue;
      bool result = Byte.TryParse(stringToConvert, out byteValue);
      if (result)
      {
         outputBlock.Text += String.Format("Converted '{0}' to {1}",
                        stringToConvert, byteValue) + "\n";
      }
      else
      {
         if (stringToConvert == null) stringToConvert = "";
         outputBlock.Text += String.Format("Attempted conversion of '{0}' failed.",
                           stringToConvert.ToString()) + "\n";
      }
   }
}
// The example displays the following output:
//       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.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.