Export (0) Print
Expand All

Convert.ToChar Method (Object)

.NET Framework 1.1

Converts the value of the specified Object to a Unicode character.

[Visual Basic]
Overloads Public Shared Function ToChar( _
   ByVal value As Object _
) As Char
[C#]
public static char ToChar(
 object value
);
[C++]
public: static __wchar_t ToChar(
 Object* value
);
[JScript]
public static function ToChar(
   value : Object
) : Char;

Parameters

value
An Object that implements the IConvertible interface.

Return Value

The Unicode character equivalent to the value of value.

-or-

Char.MinValue if value equals a null reference (Nothing in Visual Basic).

Exceptions

Exception Type Condition
InvalidCastException value does not implement the IConvertible interface.

Remarks

The return value is the result of invoking the IConvertible.ToChar method of the underlying type of value.

Example

[Visual Basic, C#, C++] The following code sample illustrates the use of ToByte, by attempting to convert a String value to a Char:

[Visual Basic] 
Public Sub ConvertStringChar(ByVal stringVal As String)
    Dim charVal As Char = "a"c

    ' A string must be one character long to convert to char.
    Try
        charVal = System.Convert.ToChar(stringVal)
        System.Console.WriteLine("{0} as a char is {1}", _
                                  stringVal, charVal)
    Catch exception As System.FormatException
        System.Console.WriteLine( _
         "The string is longer than one character.")
    Catch exception As System.ArgumentNullException
        System.Console.WriteLine("The string is null.")
    End Try

    ' A char to string conversion will always succeed.
    stringVal = System.Convert.ToString(charVal)
    System.Console.WriteLine("The character as a string is {0}", _
                              stringVal)
End Sub

[C#] 
public void ConvertStringChar(string stringVal) {
    char charVal = 'a';

    // A string must be one character long to convert to char.
    try {
        charVal = System.Convert.ToChar(stringVal);
        System.Console.WriteLine("{0} as a char is {1}",
            stringVal, charVal);
    }
    catch (System.FormatException) {
        System.Console.WriteLine(
            "The string is longer than one character.");
    }
    catch (System.ArgumentNullException) {
        System.Console.WriteLine("The string is null.");
    }

    // A char to string conversion will always succeed.
    stringVal = System.Convert.ToString(charVal);
    System.Console.WriteLine("The character as a string is {0}",
            stringVal);
}

[C++] 

   void ConvertStringChar(String* stringVal)
   {
      Char charVal = 'a';

      // A String* must be one character long to convert to char.
      try {
         charVal = System::Convert::ToChar(stringVal);
         System::Console::WriteLine(S" {0} as a char is {1}",
            stringVal, __box(charVal));
      } catch (System::FormatException*) {
         System::Console::WriteLine(S"The String* is longer than one character.");
      } catch (System::ArgumentNullException*) {
         System::Console::WriteLine(S"The String* is 0.");
      }

      // A char to String* conversion will always succeed.
      stringVal = System::Convert::ToString(charVal);
      System::Console::WriteLine(S"The character as a String* is {0}",
         stringVal);
   }

[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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Convert Class | Convert Members | System Namespace | Convert.ToChar Overload List

Show:
© 2014 Microsoft