Export (0) Print
Expand All

SByte.Parse Method

Converts the string representation of a number to its 8-bit signed integer equivalent.

The SByte type is not CLS-compliant. The CLS-compliant alternative type is Int16. For more information about CLS compliance, see What is the Common Language Specification.

Overload List

Converts the string representation of a number to its 8-bit signed integer equivalent. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Parse(String) As SByte
[C#] public static sbyte Parse(string);
[C++] public: static char Parse(String*);
[JScript] public static function Parse(String) : SByte;

Converts the string representation of a number in a specified culture-specific format to its 8-bit signed integer equivalent. This method is not CLS-compliant.

[Visual Basic] Overloads Public Shared Function Parse(String, IFormatProvider) As SByte
[C#] public static sbyte Parse(string, IFormatProvider);
[C++] public: static char Parse(String*, IFormatProvider*);
[JScript] public static function Parse(String, IFormatProvider) : SByte;

Converts the string representation of a number in a specified style to its 8-bit signed integer equivalent. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Parse(String, NumberStyles) As SByte
[C#] public static sbyte Parse(string, NumberStyles);
[C++] public: static char Parse(String*, NumberStyles);
[JScript] public static function Parse(String, NumberStyles) : SByte;

Converts the string representation of a number in a specified style and culture-specific format to its 8-bit signed integer equivalent. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Parse(String, NumberStyles, IFormatProvider) As SByte
[C#] public static sbyte Parse(string, NumberStyles, IFormatProvider);
[C++] public: static char Parse(String*, NumberStyles, IFormatProvider*);
[JScript] public static function Parse(String, NumberStyles, IFormatProvider) : SByte;

Example

[Visual Basic, C#, C++] The following code example parses string representations of SByte (signed byte) values with several overloads of the Parse method.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Parse. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Example of the SByte.Parse( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module SByteParseDemo
    
    Sub SByteParse( styles As NumberStyles, _
        provider As IFormatProvider )

        Dim sbyteFormats As String( ) = { _
            " 99 ",     " +123 ",   " (123) ", _
            " -123 ",   " 1_2_3",   " 7E " }
            
        ' Parse each string in the sbyteFormats array, using 
        ' NumberStyles and IFormatProvider, if specified.
        Dim sbyteString As String
        For Each sbyteString In  sbyteFormats

            Dim sbyteNumber As SByte
                
            ' Display the first part of the output line.
            Console.Write( "  Parse of {0,-12}", _
                String.Format( """{0}""", sbyteString ) )

            ' Use the appropriate SByte.Parse overload, based on 
            ' the parameters that are specified.
            Try
                If provider Is Nothing Then
                    If styles < 0 Then
                        sbyteNumber = SByte.Parse( sbyteString )
                    Else
                        sbyteNumber = _
                            SByte.Parse( sbyteString, styles )
                    End If
                ElseIf styles < 0 Then
                    sbyteNumber = _
                        SByte.Parse( sbyteString, provider )
                Else
                    sbyteNumber = SByte.Parse( _
                        sbyteString, styles, provider )
                End If
                
                ' Display the resulting value if Parse succeeded.
                Console.WriteLine( "succeeded: {0}", _
                    sbyteNumber )

            ' Display the exception message if Parse failed.
            Catch ex As Exception
                Console.WriteLine( "failed: {0}", ex.Message )
            End Try
        Next sbyteString
    End Sub 
        
    Sub RunParseDemo( )
            
        ' Do not use IFormatProvider or NumberStyles.
        Console.WriteLine( vbCrLf & _
            "NumberStyles and IFormatProvider are not used:" )
        SByteParse( CType( -1, NumberStyles ), Nothing )
            
        ' Use NumberStyles.HexNumber; do not use IFormatProvider.
        Console.WriteLine( vbCrLf & "NumberStyles." & _
            "HexNumber is used; IFormatProvider is not used:" )
        SByteParse( NumberStyles.HexNumber, Nothing )
            
        ' Get the NumberFormatInfo object from the invariant 
        ' culture, and enable parentheses to indicate negative.
        Dim culture As New CultureInfo( "" )
        Dim numFormat As NumberFormatInfo = culture.NumberFormat
        numFormat.NumberNegativePattern = 0
            
        ' Change the digit group separator to '_' and the digit
        ' group size to 1.
        numFormat.NumberGroupSeparator = "_"
        numFormat.NumberGroupSizes = New Integer( ) { 1 }
            
        ' Use the NumberFormatInfo object as the IFormatProvider.
        Console.WriteLine( vbCrLf & _
            "A NumberStyles value is not used, but the " & _
            "IFormatProvider sets the group " & vbCrLf & "" & _
            "separator = '_', group size = 1, and negative " & _
            "pattern = ( ):" )
        SByteParse( CType( -1, NumberStyles ), numFormat )
            
        ' Use NumberStyles.Number and NumberStyles.AllowParentheses.
        Console.WriteLine( vbCrLf & _
            "NumberStyles.Number, NumberStyles.AllowParentheses, " & _
            "and the same " & vbCrLf & "IFormatProvider are used:" )
        SByteParse( NumberStyles.Number + _
            NumberStyles.AllowParentheses, numFormat )
    End Sub 
        
    Sub Main( )
        Console.WriteLine( "This example of" & vbCrLf & _
            "  SByte.Parse( String )," & vbCrLf & _
            "  SByte.Parse( String, NumberStyles )," & vbCrLf & _
            "  SByte.Parse( String, IFormatProvider ), and" & _
            vbCrLf & "  SByte.Parse( String, NumberStyles, " & _
            "IFormatProvider )" & vbCrLf & _
            "generates the following output when parsing " & _
            "string representations" & vbCrLf & "of SByte " & _
            "values with each of these forms of SByte.Parse( )." )
            
        RunParseDemo( )

    End Sub 
End Module 

' This example of
'   SByte.Parse( String ),
'   SByte.Parse( String, NumberStyles ),
'   SByte.Parse( String, IFormatProvider ), and
'   SByte.Parse( String, NumberStyles, IFormatProvider )
' generates the following output when parsing string representations
' of SByte values with each of these forms of SByte.Parse( ).
' 
' NumberStyles and IFormatProvider are not used:
'   Parse of " 99 "      succeeded: 99
'   Parse of " +123 "    succeeded: 123
'   Parse of " (123) "   failed: Input string was not in a correct format.
'   Parse of " -123 "    succeeded: -123
'   Parse of " 1_2_3"    failed: Input string was not in a correct format.
'   Parse of " 7E "      failed: Input string was not in a correct format.
' 
' NumberStyles.HexNumber is used; IFormatProvider is not used:
'   Parse of " 99 "      succeeded: -103
'   Parse of " +123 "    failed: Input string was not in a correct format.
'   Parse of " (123) "   failed: Input string was not in a correct format.
'   Parse of " -123 "    failed: Input string was not in a correct format.
'   Parse of " 1_2_3"    failed: Input string was not in a correct format.
'   Parse of " 7E "      succeeded: 126
' 
' A NumberStyles value is not used, but the IFormatProvider sets the group
' separator = '_', group size = 1, and negative pattern = ( ):
'   Parse of " 99 "      succeeded: 99
'   Parse of " +123 "    succeeded: 123
'   Parse of " (123) "   failed: Input string was not in a correct format.
'   Parse of " -123 "    succeeded: -123
'   Parse of " 1_2_3"    failed: Input string was not in a correct format.
'   Parse of " 7E "      failed: Input string was not in a correct format.
' 
' NumberStyles.Number, NumberStyles.AllowParentheses, and the same
' IFormatProvider are used:
'   Parse of " 99 "      succeeded: 99
'   Parse of " +123 "    succeeded: 123
'   Parse of " (123) "   succeeded: -123
'   Parse of " -123 "    succeeded: -123
'   Parse of " 1_2_3"    succeeded: 123
'   Parse of " 7E "      failed: Input string was not in a correct format.

[C#] 
// Example of the SByte.Parse( ) methods.
using System;
using System.Globalization;

class SByteParseDemo
{
    static void SByteParse( NumberStyles styles, 
        IFormatProvider provider )
    {
        string[ ] sbyteFormats = {
            " 99 ",     " +123 ",   " (123) ", 
            " -123 ",   " 1_2_3",   " 7E " };
            
        // Parse each string in the sbyteFormats array, using 
        // NumberStyles and IFormatProvider, if specified.
        foreach( string sbyteString in sbyteFormats )
        {
            SByte sbyteNumber;
                
            // Display the first part of the output line.
            Console.Write( "  Parse of {0,-12}", 
                String.Format( "\"{0}\"", sbyteString ) );

            try
            {
                // Use the appropriate SByte.Parse overload, based 
                // on the parameters that are specified.
                if( provider == null )
                    if( styles < 0 )
                        sbyteNumber = SByte.Parse( sbyteString );
                    else
                        sbyteNumber = 
                            SByte.Parse( sbyteString, styles );
                else if( styles < 0 )
                    sbyteNumber = 
                        SByte.Parse( sbyteString, provider );
                else
                    sbyteNumber = SByte.Parse(
                        sbyteString, styles, provider );
                
                // Display the resulting value if Parse succeeded.
                Console.WriteLine( "succeeded: {0}", 
                    sbyteNumber );
            }
            catch( Exception ex )
            {
                // Display the exception message if Parse failed.
                Console.WriteLine( "failed: {0}", ex.Message );
            }
        }
    } 
        
    static void RunParseDemo( )
    {
        // Do not use IFormatProvider or NumberStyles.
        Console.WriteLine(
            "\nNumberStyles and IFormatProvider are not used:" );
        SByteParse( (NumberStyles)(-1), null );
            
        // Use NumberStyles.HexNumber; do not use IFormatProvider.
        Console.WriteLine( "\nNumberStyles.HexNumber " +
            "is used; IFormatProvider is not used:" );
        SByteParse( NumberStyles.HexNumber, null );
            
        // Get the NumberFormatInfo object from the invariant 
        // culture, and enable parentheses to indicate negative.
        CultureInfo         culture = new CultureInfo( "" );
        NumberFormatInfo    numFormat = culture.NumberFormat;
        numFormat.NumberNegativePattern = 0;
            
        // Change the digit group separator to '_' and the digit
        // group size to 1.
        numFormat.NumberGroupSeparator = "_";
        numFormat.NumberGroupSizes = new int[ ] { 1 };
            
        // Use the NumberFormatInfo object as the IFormatProvider.
        Console.WriteLine( "\nA NumberStyles value is not used, " +
            "but the IFormatProvider sets the group \nseparator = " +
            "'_', group size = 1, and negative pattern = ( ):" );
        SByteParse( (NumberStyles)(-1), numFormat );
            
        // Use NumberStyles.Number and NumberStyles.AllowParentheses.
        Console.WriteLine( 
            "\nNumberStyles.Number, NumberStyles.AllowParentheses, " +
            "and the same \nIFormatProvider are used:" );
        SByteParse( NumberStyles.Number | 
            NumberStyles.AllowParentheses, numFormat );
    } 
        
    static void Main( )
    {
        Console.WriteLine( "This example of\n" +
            "  SByte.Parse( String ),\n" +
            "  SByte.Parse( String, NumberStyles ),\n" +
            "  SByte.Parse( String, IFormatProvider ), and\n" +
            "  SByte.Parse( String, NumberStyles, IFormatProvider )" +
            "\ngenerates the following output when parsing " +
            "string representations\nof SByte values with each " +
            "of these forms of SByte.Parse( )." );
            
        RunParseDemo( );
    } 
} 

/*
This example of
  SByte.Parse( String ),
  SByte.Parse( String, NumberStyles ),
  SByte.Parse( String, IFormatProvider ), and
  SByte.Parse( String, NumberStyles, IFormatProvider )
generates the following output when parsing string representations
of SByte values with each of these forms of SByte.Parse( ).

NumberStyles and IFormatProvider are not used:
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      failed: Input string was not in a correct format.

NumberStyles.HexNumber is used; IFormatProvider is not used:
  Parse of " 99 "      succeeded: -103
  Parse of " +123 "    failed: Input string was not in a correct format.
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    failed: Input string was not in a correct format.
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      succeeded: 126

A NumberStyles value is not used, but the IFormatProvider sets the group
separator = '_', group size = 1, and negative pattern = ( ):
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      failed: Input string was not in a correct format.

NumberStyles.Number, NumberStyles.AllowParentheses, and the same
IFormatProvider are used:
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   succeeded: -123
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    succeeded: 123
  Parse of " 7E "      failed: Input string was not in a correct format.
*/ 

[C++] 
// Example of the SByte::Parse( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
using namespace System::Collections;

void SByteParse( NumberStyles styles, IFormatProvider* provider )
{
    String* sbyteFormats [ ] = __gc new String*[ 6 ];
        sbyteFormats[ 0 ] = S" 99 ";
        sbyteFormats[ 1 ] = S" +123 ";
        sbyteFormats[ 2 ] = S" (123) ";
        sbyteFormats[ 3 ] = S" -123 ";
        sbyteFormats[ 4 ] = S" 1_2_3";
        sbyteFormats[ 5 ] = S" 7E ";
        
    // Parse each string in the sbyteFormats array, using 
    // NumberStyles and IFormatProvider, if specified.
    // Implements foreach( String* sbyteString in sbyteFormats ).
    IEnumerator*    myEnum = sbyteFormats->GetEnumerator( );
    while( myEnum->MoveNext( ) )
    {
        String* sbyteString = __try_cast<String*>( myEnum->Current );
        SByte   sbyteNumber;
            
            // Display the first part of the output line.
        Console::Write( "  Parse of {0,-12}", 
            String::Format( "\"{0}\"", sbyteString ) );

        try
        {
            // Use the appropriate SByte.Parse overload, based 
            // on the parameters that are specified.
            if( provider == (IFormatProvider*)0 )
                if( styles < 0 )
                    sbyteNumber = SByte::Parse( sbyteString );
                else
                    sbyteNumber = SByte::Parse( sbyteString, styles );
            else if( styles < 0 )
                sbyteNumber = SByte::Parse( sbyteString, provider );
            else
                sbyteNumber = 
                    SByte::Parse( sbyteString, styles, provider );
            
            // Display the resulting value if Parse succeeded.
            Console::WriteLine( S"succeeded: {0}", 
                __box( sbyteNumber ) );
        }
        catch( Exception* ex )
        {
            // Display the exception message if Parse failed.
            Console::WriteLine( S"failed: {0}", ex->Message );
        }
    }
} 
    
void RunParseDemo( )
{
    IFormatProvider* null = 0;

    // Do not use IFormatProvider or NumberStyles.
    Console::WriteLine( 
        S"\nNumberStyles and IFormatProvider are not used:" );
    SByteParse( (NumberStyles)(-1), null );
        
    // Use NumberStyles::HexNumber; do not use IFormatProvider.
    Console::WriteLine( S"\nNumberStyles::HexNumber " 
        S"is used; IFormatProvider is not used:" );
    SByteParse( NumberStyles::HexNumber, null );
        
    // Get the NumberFormatInfo object from the invariant 
    // culture, and enable parentheses to indicate negative.
    CultureInfo*        culture = new CultureInfo( S"" );
    NumberFormatInfo*   numFormat = culture->NumberFormat;
    numFormat->NumberNegativePattern = 0;
        
    // Change the digit group separator to '_' and the digit
    // group size to 1.
    numFormat->NumberGroupSeparator = S"_";
    Int32 sizes __gc [] = { 1 };
    numFormat->NumberGroupSizes = sizes;
        
    // Use the NumberFormatInfo object as the IFormatProvider.
    Console::WriteLine( S"\nA NumberStyles value is not used, " 
        S"but the IFormatProvider sets the group \nseparator = " 
        S"'_', group size = 1, and negative pattern = ( ):" );
    SByteParse( (NumberStyles)(-1), numFormat );
        
    // Use NumberStyles::Number and NumberStyles::AllowParentheses.
    Console::WriteLine( 
        S"\nNumberStyles::Number, NumberStyles::AllowParentheses, " 
        S"and the same \nIFormatProvider are used:" );
    SByteParse( (NumberStyles)(NumberStyles::Number | 
        NumberStyles::AllowParentheses), numFormat );
} 
    
void main( )
{
    Console::WriteLine( S"This example of\n" 
        S"  SByte::Parse( String* ),\n" 
        S"  SByte::Parse( String*, NumberStyles ),\n" 
        S"  SByte::Parse( String*, IFormatProvider* ), and\n" 
        S"  SByte::Parse( String*, NumberStyles, IFormatProvider* )" 
        S"\ngenerates the following output when " 
        S"parsing string representations\nof SByte " 
        S"values with each of these forms of SByte::Parse( )." );
        
    RunParseDemo( );
}

/*
This example of
  SByte::Parse( String* ),
  SByte::Parse( String*, NumberStyles ),
  SByte::Parse( String*, IFormatProvider* ), and
  SByte::Parse( String*, NumberStyles, IFormatProvider* )
generates the following output when parsing string representations
of SByte values with each of these forms of SByte::Parse( ).

NumberStyles and IFormatProvider are not used:
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      failed: Input string was not in a correct format.

NumberStyles::HexNumber is used; IFormatProvider is not used:
  Parse of " 99 "      succeeded: -103
  Parse of " +123 "    failed: Input string was not in a correct format.
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    failed: Input string was not in a correct format.
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      succeeded: 126

A NumberStyles value is not used, but the IFormatProvider sets the group
separator = '_', group size = 1, and negative pattern = ( ):
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      failed: Input string was not in a correct format.

NumberStyles::Number, NumberStyles::AllowParentheses, and the same
IFormatProvider are used:
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   succeeded: -123
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    succeeded: 123
  Parse of " 7E "      failed: Input string was not in a correct format.
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

SByte Structure | SByte Members | System Namespace

Show:
© 2014 Microsoft