asp.net.ph

Enum.ToString Method ( )

System Namespace   Enum Class


Converts the value of this instance to its equivalent string representation.

[ VB ]
<Serializable>
Overrides Overloads Public Function ToString ( ) As String

[ C# ]
[Serializable]
public override string ToString ( );

[ C++ ]
[Serializable]
public: String* ToString ( );

[JScript ]
public Serializable
override function ToString ( ) : String;

Return Value

The string representation of the value of this instance.

Remarks

This method works as if the general format character, "G", were specified. That is, if the FlagsAttribute is not applied to this enumerated type and there is a named constant equal to the value of this instance, then the return value is a string containing the name of the constant. If the FlagsAttribute is applied and there is a combination of one or more named constants equal to the value of this instance, then the return value is a string containing a delimiter-separated list of the names of the constants. Otherwise, the return value is the string representation of the numeric value of this instance.

For more information about format characters, see the Remarks section of the Format method. For more information about formatting in general, see Formatting Overview.

Example

The following code example demonstrates converting an enumerated value to a string.

[ VB ] 
Imports System

Public Class EnumSample    
    Enum Colors
        Red = 1
        Blue = 2
    End Enum
    
    Public Shared Sub Main ( ) 
        Dim myColors As Colors = Colors.Red
        Response.WriteLine ( "The value of this instance is '{0}'", _
           myColors.ToString ( ) ) 
    End Sub
End Class

'Output.
'The value of this instance is 'Red'.

[ C# ] 
using System;

public class EnumSample {
    enum Colors {Red = 1, Blue = 2};
    
    public static void Main ( ) {
        Enum myColors = Colors.Red;
        Response.WriteLine ( "The value of this instance is '{0}'",
           myColors.ToString ( ) );
    }
}
/*
Output.
The value of this instance is 'Red'.
*/

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

See Also

Enum Members   Enum.ToString Overload List   Format   Parse Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph