468,250 Members | 1,583 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,250 developers. It's quick & easy.

enum to string, several custom formats

I would like to be able to convert enums to their string representation but
I would like to be able to apply a custom formatting, so that I could write
for example:

enum E { a, b, c };

string s1 = E.a.ToString( "d" ); // returns for example 'a'
string s2 = E.a.ToString( "f" ); // returns for example 'this is an enum
value a'

for now I have several methods in special class

class EnumToString
{
string ConvertAToStringD { return ...; }
string ConvertAToStringF { return ...; }
}

but this looks rather ugly.

I've read about EnumConverter class but I am not sure if it is possible to
have several different custom formatting possibilities when using this
class.

could anyone enlighten me?

Thanks in advance.
Wiktor Zychla
Nov 15 '05 #1
3 3118
Hi Wiktor,

I've never used EnumConverters but I looked up the Help topic.

This is how one of these things seems to be used:

Enum eMyBike = eMotorBikes.HondaSuperBlackbird;
string s = "I love to ride my " + TypeDescriptor.GetConverter
(eMyBike).ConvertTo (eMyBike, typeof(string));

Now <that's> what I call ugly!! [not the bike - that's beautiful :-)]

My opinion is that you should do whatever's necessary in your class,
clumsy or not, so that the class's <user> can have pretty code.

Or maybe you want to use the EnumConverter inside your class??

Just some thoughts :-)

Regards,
Fergus
Nov 15 '05 #2
> string s1 = E.a.ToString( "d" ); // returns for example 'a'
string s2 = E.a.ToString( "f" ); // returns for example 'this is an enum

dobrze jak by sie dało przeciążyć funkcje w typie wyliczeniowym
ale... sie nie da

może napisać własną klasę formatującą np.:

using System;

public class mF : IFormatProvider, ICustomFormatter
{
public object GetFormat (Type service)
{
if (service == typeof (ICustomFormatter))
{
return this;
}
else
{
return null;
}
}
public string Format (string format, object arg, IFormatProvider
provider)
{
if(format == "F")
{
return "This is an enum value: " + arg.ToString();
}
return String.Format("{0:" + format + "}",arg);
}
}

public enum E
{

a,b,c
}

public class Test
{
//[STAThread]
static void Main()
{
Console.WriteLine(String.Format(new mF(), "{0:S}, {1:F},{2:D}"
,E.c,E.b,E.a));
}
}

--
Selvin - Przemek Sulikowski
se****@osadkowski.com.pl
Nov 15 '05 #3
> dobrze jak by sie dało przeciążyć funkcje w typie wyliczeniowym
ale... sie nie da
exactly.

może napisać własną klasę formatującą np.:


this looks really promising.

thanks ;)
Wiktor


Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Kaila | last post: by
5 posts views Thread by John Smith | last post: by
21 posts views Thread by Andreas Huber | last post: by
3 posts views Thread by cyshao | last post: by
31 posts views Thread by Michael C | last post: by
7 posts views Thread by Brian Mitchell | last post: by
2 posts views Thread by Martin Engelhardt | last post: by
34 posts views Thread by Steven Nagy | last post: by
11 posts views Thread by =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.