Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Convert.ToSByte Method (String, IFormatProvider)

Converts the specified String representation of a number to an equivalent 8-bit signed integer using specified culture-specific formatting information.

This method is not CLS-compliant.  

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

[CLSCompliantAttribute(false)] 
public static sbyte ToSByte (
	string value,
	IFormatProvider provider
)
/** @attribute CLSCompliantAttribute(false) */ 
public static SByte ToSByte (
	String value, 
	IFormatProvider provider
)
CLSCompliantAttribute(false) 
public static function ToSByte (
	value : String, 
	provider : IFormatProvider
) : sbyte

Parameters

value

A String containing a number to convert.

provider

An IFormatProvider interface implementation that supplies culture-specific formatting information.

Return Value

An 8-bit signed integer equivalent to the value of value.

Exception typeCondition

ArgumentNullException

value is a null reference (Nothing in Visual Basic).

FormatException

value does not consist of an optional sign followed by a sequence of digits (zero through nine).

OverflowException

value represents a number less than MinValue or greater than MaxValue.

provider is an IFormatProvider instance that obtains a NumberFormatInfo object. The NumberFormatInfo object provides culture-specific information about the format of value. If provider is a null reference (Nothing in Visual Basic), the NumberFormatInfo for the current culture is used.

The following code example converts String representations of SByte (signed byte) values with the ToSByte method, using an IFormatProvider object.

// Example of the Convert.ToSByte( string ) and 
// Convert.ToSByte( string, IFormatProvider ) methods.
using System;
using System.Globalization;

class ToSByteProviderDemo
{
    static string format = "{0,-20}{1,-20}{2}";

     // Get the exception type name; remove the namespace prefix.
    static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    static void ConvertToSByte( string numericStr, 
        IFormatProvider provider )
    {
        object defaultValue;
        object providerValue;

        // Convert numericStr to SByte without a format provider.
        try
        {
            defaultValue = Convert.ToSByte( numericStr );
        }
        catch( Exception ex )
        {
            defaultValue = GetExceptionType( ex );
        }

        // Convert numericStr to SByte with a format provider.
        try
        {
            providerValue = Convert.ToSByte( numericStr, provider );
        }
        catch( Exception ex )
        {
            providerValue = GetExceptionType( ex );
        }

        Console.WriteLine( format, numericStr, 
            defaultValue, providerValue );
    }

    public static void Main( )
    {
        // Create a NumberFormatInfo object and set several of its
        // properties that apply to numbers.
        NumberFormatInfo provider = new NumberFormatInfo();

        // These properties affect the conversion.
        provider.NegativeSign = "neg ";
        provider.PositiveSign = "pos ";

        // These properties do not affect the conversion.
        // The input string cannot have decimal and group separators.
        provider.NumberDecimalSeparator = ".";
        provider.NumberNegativePattern = 0;

        Console.WriteLine("This example of\n" +
            "  Convert.ToSByte( string ) and \n" +
            "  Convert.ToSByte( string, IFormatProvider ) " +
            "\ngenerates the following output. It converts " +
            "several strings to \nSByte values, using " +
            "default formatting or a NumberFormatInfo object.\n" );
        Console.WriteLine( format, "String to convert", 
            "Default/exception", "Provider/exception" );
        Console.WriteLine( format, "-----------------", 
            "-----------------", "------------------" );

        // Convert strings, with and without an IFormatProvider.
        ConvertToSByte( "123", provider );
        ConvertToSByte( "+123", provider );
        ConvertToSByte( "pos 123", provider );
        ConvertToSByte( "-123", provider );
        ConvertToSByte( "neg 123", provider );
        ConvertToSByte( "123.", provider );
        ConvertToSByte( "(123)", provider );
        ConvertToSByte( "128", provider );
        ConvertToSByte( "-129", provider );
    }
}

/*
This example of
  Convert.ToSByte( string ) and
  Convert.ToSByte( string, IFormatProvider )
generates the following output. It converts several strings to
SByte values, using default formatting or a NumberFormatInfo object.

String to convert   Default/exception   Provider/exception
-----------------   -----------------   ------------------
123                 123                 123
+123                123                 FormatException
pos 123             FormatException     123
-123                -123                FormatException
neg 123             FormatException     -123
123.                FormatException     FormatException
(123)               FormatException     FormatException
128                 OverflowException   OverflowException
-129                OverflowException   FormatException
*/ 

// Example of the Convert.ToSByte( string ) and 
// Convert.ToSByte( string, IFormatProvider ) methods.
import System.* ;
import System.Globalization.* ;

class ToSByteProviderDemo
{
    private static String format = "{0,-20}{1,-20}{2}";   
   
    // Get the exception type name; remove the namespace prefix.
    static String GetExceptionType(System.Exception ex) 
    {
        String exceptionType = ex.GetType().ToString();
        return exceptionType.Substring((exceptionType.LastIndexOf('.') + 1)) ;
    } //GetExceptionType   
   
    static void ConvertToSByte(String numericStr, IFormatProvider provider) 
    {
        Object defaultValue;
        Object providerValue;
      
        // Convert numericStr to SByte without a format provider.
        try {
            defaultValue = System.Convert.ToString(Convert.ToSByte(numericStr));
        }
        catch(System.Exception  ex) {      
            defaultValue = GetExceptionType(ex);
        }
      
        // Convert numericStr to SByte with a format provider.
        try {
            providerValue = System.Convert.ToString(
                            Convert.ToSByte(numericStr, provider));
        }
        catch(System.Exception  ex) {      
            providerValue = GetExceptionType(ex);
        }
      
        Console.WriteLine(format, numericStr, defaultValue, providerValue);
    } //ConvertToSByte   
   
    public static void main(String[] args)
    {
        // Create a NumberFormatInfo object and set several of its
        // properties that apply to numbers.
        NumberFormatInfo provider =  new NumberFormatInfo();
      
        // These properties affect the conversion.
        provider.set_NegativeSign("neg ");
        provider.set_PositiveSign("pos ");
      
        // These properties do not affect the conversion.
        // The input string cannot have decimal and group separators.
        provider.set_NumberDecimalSeparator(".");
        provider.set_NumberNegativePattern(0);
      
        Console.WriteLine(("This example of\n" 
            + "  Convert.ToSByte( string ) and \n" 
            + "  Convert.ToSByte( string, IFormatProvider ) " 
            + "\ngenerates the following output. It converts " 
            + "several strings to \nSByte values, using " 
            + "default formatting or a NumberFormatInfo object.\n"));
        Console.WriteLine(format, "String to convert", "Default/exception", 
            "Provider/exception");
        Console.WriteLine(format, "-----------------", "-----------------", 
            "------------------");
      
        // Convert strings, with and without an IFormatProvider.
        ConvertToSByte("123", provider);
        ConvertToSByte("+123", provider);
        ConvertToSByte("pos 123", provider);
        ConvertToSByte("-123", provider);
        ConvertToSByte("neg 123", provider);
        ConvertToSByte("123.", provider);
        ConvertToSByte("(123)", provider);
        ConvertToSByte("128", provider);
        ConvertToSByte("-129", provider);
    } //main
} //ToSByteProviderDemo

/*
This example of
  Convert.ToSByte( string ) and
  Convert.ToSByte( string, IFormatProvider )
generates the following output. It converts several strings to
SByte values, using default formatting or a NumberFormatInfo object.

String to convert   Default/exception   Provider/exception
-----------------   -----------------   ------------------
123                 123                 123
+123                123                FormatException
pos 123            FormatException     123
-123                -123                FormatException
neg 123             FormatException    -123
123.                FormatException     FormatException
(123)               FormatException     FormatException
128                 OverflowException   OverflowException
-129                OverflowException   FormatException
Press any key to continue
*/

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft