Software Development Network>> Visual C#>> overriding ToString () method for enumerations in C#
Franz,
Unfortunately it's not possible to override ToString() on a enumeration. Instead, you need to provide a helper method to do this:
Hallo rabinyour example overrides the ToString() method of a class.But I have to override the method of an enumeration type.My example:class ImportItem{ public enum ImportState { ImportIt, DontImportIt, Importing, Imported, ImportFailed } public ImportState State { get { return mState; } } private ImportState mState; .....}class Test{ public static void Main () { ImportItem Item = new ImportItem (); ..... Console.WriteLine (Item.State); }}This will output "ImportIt", but I would like to override ToString () such that it outputs i.e. "Import required". Thanks for a tipFranz
overriding ToString () method for enumerations in C#
Seshu Palachulla
Franz,
Unfortunately it's not possible to override ToString() on a enumeration. Instead, you need to provide a helper method to do this:
public static string ToString(MyEnum value)
{
return "This is my customized ToString():" + value.ToString();
}
frozennose
TGAR
http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemflagsattributeclasstopic.asp
tom-b
Hallo rabin
your example overrides the ToString() method of a class.
But I have to override the method of an enumeration type.
My example:
class ImportItem
{
public enum ImportState { ImportIt, DontImportIt, Importing, Imported, ImportFailed }
public ImportState State { get { return mState; } }
private ImportState mState;
.....
}
class Test
{
public static void Main ()
{
ImportItem Item = new ImportItem ();
.....
Console.WriteLine (Item.State);
}
}
This will output "ImportIt", but I would like to override ToString () such that it outputs i.e. "Import required".
Thanks for a tip
Franz