Sdílet prostřednictvím


SByte.TryParse Method (String, SByte%)

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

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

This API is not CLS-compliant. 

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

Syntax

'Declaration
<CLSCompliantAttribute(False)> _
Public Shared Function TryParse ( _
    s As String, _
    <OutAttribute> ByRef result As SByte _
) As Boolean
[CLSCompliantAttribute(false)]
public static bool TryParse(
    string s,
    out sbyte result
)

Parameters

  • s
    Type: System.String
    A string that contains a number to convert.
  • result
    Type: System.SByte%
    When this method returns, contains the 8-bit signed integer value that is equivalent to the number contained in s if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is nulla null reference (Nothing in Visual Basic), is not in the correct format, or represents a number that is less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

Return Value

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

Remarks

The SByte.TryParse(String, SByte%) method is like the SByte.Parse(String) method, except that it does not throw an exception if the conversion fails. This method eliminates the need to use exception handling to test for a FormatException if value is invalid and cannot be successfully parsed.

The s parameter should be the string representation of a decimal 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 sign. Valid sign characters are determined by the NumberFormatInfo.NegativeSign and NumberFormatInfo.PositiveSign properties of the current culture.

digits

A sequence of decimal digits ranging from 0 to 9.

NoteNote:

The string specified by the value parameter cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

The s parameter is interpreted by using the NumberStyles.Integer style. In addition to the decimal digits, only leading and trailing spaces with a leading sign are allowed. To explicitly define the style elements with the culture-specific formatting information that can be present in value, call the TryParse(String, NumberStyles, IFormatProvider, SByte%) method.

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

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

Examples

The following example tries to convert the strings in an array to SByte values by calling the TryParse(String, SByte%) method.

Dim numericStrings() As String = {"-3.6", "12.8", "+16.7", "    3   ", _
                                  "(17)", "-17", "+12", "18-", "987", _
                                  "1,024", "  127 "}
Dim number As SByte
For Each numericString As String In numericStrings
   If SByte.TryParse(numericString, number) Then
      outputBlock.Text += String.Format("Converted '{0}' to {1}.", numericString, number) + vbCrLf
   Else
      outputBlock.Text += String.Format("Cannot convert '{0}' to an SByte.", numericString) + vbCrLf
   End If
Next
' The example displays the following output:
'       Cannot convert '-3.6' to an SByte.
'       Cannot convert '12.8' to an SByte.
'       Cannot convert '+16.7' to an SByte.
'       Converted '    3   ' to 3.
'       Cannot convert '(17)' to an SByte.
'       Converted '-17' to -17.
'       Converted '+12' to 12.
'       Cannot convert '18-' to an SByte.
'       Cannot convert '987' to an SByte.
'       Cannot convert '1,024' to an SByte.
'       Converted '  127 ' to 127.
string[] numericStrings = {"-3.6", "12.8", "+16.7", "    3   ", "(17)", 
                           "-17", "+12", "18-", "987", "1,024", "  127 "};
sbyte number;
foreach (string numericString in numericStrings)
{
   if (sbyte.TryParse(numericString, out number))
      outputBlock.Text += String.Format("Converted '{0}' to {1}.", numericString, number) + "\n";
   else
      outputBlock.Text += String.Format("Cannot convert '{0}' to an SByte.", numericString) + "\n";
}
// The example displays the following output:
//       Cannot convert '-3.6' to an SByte.
//       Cannot convert '12.8' to an SByte.
//       Cannot convert '+16.7' to an SByte.
//       Converted '    3   ' to 3.
//       Cannot convert '(17)' to an SByte.
//       Converted '-17' to -17.
//       Converted '+12' to 12.
//       Cannot convert '18-' to an SByte.
//       Cannot convert '987' to an SByte.
//       Cannot convert '1,024' to an SByte.
//       Converted '  127 ' to 127.

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.