Enum To String

S

shapper

Hello,

I am creating a way to convert en enum to a friendly and localized
string.
I am using the following Type Converter:

public class MyEnumToString : TypeConverter {

// CanConvertFrom
public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
return typeof(MyEnum).IsAssignableFrom(sourceType);
} // CanConvertFrom

// CanConvertTo
public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
return destinationType == typeof(String);
} // CanConvertTo

// ConvertTo
public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
MyEnum myEnum = (MyEnum)value;
switch (myEnum) {
case MyEnum.AAA:
return "This is AAA description";
case MyEnum.BBB:
return "This is BBB description";
default:
return String.Empty;
}
} // ConvertTo

} // MyEnumToString

And a String extension named ToStringType:

public static class StringExtensions {
public static string ToStringType<T>(this T o) {
var converter = System.ComponentModel.TypeDescriptor.GetConverter
(o);
if (o != null) return converter.ConvertToString(o);
return Convert.ToString(o);
}
}

Then I use, for example:
String aaa = MyEnum.AAA.ToStringType();

And aaa will become "This is AAA description".

This is working. My question is:
- Is this the best way to implement this?
- And is there a way to use the default ToString method?
(I am able to use String aaa = MyEnum.AAA.ToString() but I get
"AAA").

I have use Type converters to convert a List<T> to a CSV String. But
not enums.

Thanks,
Miguel
 
D

Dude

Hello,

I am creating a way to convert en enum to a friendly and localized
string.
I am using the following Type Converter:

public class MyEnumToString : TypeConverter {

  // CanConvertFrom
  public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
    return typeof(MyEnum).IsAssignableFrom(sourceType);
  } // CanConvertFrom

  // CanConvertTo
  public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
    return destinationType == typeof(String);
  } // CanConvertTo

  // ConvertTo
  public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
      MyEnum myEnum = (MyEnum)value;
      switch (myEnum) {
        case MyEnum.AAA:
          return "This is AAA description";
        case MyEnum.BBB:
          return "This is BBB description";
        default:
          return String.Empty;
      }
    } // ConvertTo

} // MyEnumToString

And a String extension named ToStringType:

public static class StringExtensions {
  public static string ToStringType<T>(this T o) {
    var converter = System.ComponentModel.TypeDescriptor.GetConverter
(o);
    if (o != null) return converter.ConvertToString(o);
    return Convert.ToString(o);
  }

}

Then I use, for example:
String aaa = MyEnum.AAA.ToStringType();

And aaa will become "This is AAA description".

This is working. My question is:
- Is this the best way to implement this?
- And is there a way to use the default ToString method?
  (I am able to use String aaa = MyEnum.AAA.ToString() but I get
"AAA").

I have use Type converters to convert a List<T> to a CSV String. But
not enums.

Thanks,
Miguel

Is there a reason this wouldn't suffice?

namespace ConsoleApplication1
{
enum Bob { A, B, C };

static class TypeConverter
{
public static string ToString(Bob myTypeX)
{
return string.Format("This is a type ({0})",
myTypeX.ToString());
}
}


class Program
{
static void Main(string[] args)
{
Bob aVariable = Bob.B;

System.Console.WriteLine(aVariable.ToString());

System.Console.WriteLine(TypeConverter.ToString
(aVariable));
}
}
}
 
D

Dude

Hello,

I am creating a way to convert en enum to a friendly and localized
string.
I am using the following Type Converter:

public class MyEnumToString : TypeConverter {

  // CanConvertFrom
  public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
    return typeof(MyEnum).IsAssignableFrom(sourceType);
  } // CanConvertFrom

  // CanConvertTo
  public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
    return destinationType == typeof(String);
  } // CanConvertTo

  // ConvertTo
  public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
      MyEnum myEnum = (MyEnum)value;
      switch (myEnum) {
        case MyEnum.AAA:
          return "This is AAA description";
        case MyEnum.BBB:
          return "This is BBB description";
        default:
          return String.Empty;
      }
    } // ConvertTo

} // MyEnumToString

And a String extension named ToStringType:

public static class StringExtensions {
  public static string ToStringType<T>(this T o) {
    var converter = System.ComponentModel.TypeDescriptor.GetConverter
(o);
    if (o != null) return converter.ConvertToString(o);
    return Convert.ToString(o);
  }

}

Then I use, for example:
String aaa = MyEnum.AAA.ToStringType();

And aaa will become "This is AAA description".

This is working. My question is:
- Is this the best way to implement this?
- And is there a way to use the default ToString method?
  (I am able to use String aaa = MyEnum.AAA.ToString() but I get
"AAA").

I have use Type converters to convert a List<T> to a CSV String. But
not enums.

Thanks,
Miguel

Is there a reason this wouldn't suffice?

namespace ConsoleApplication1
{
enum Bob { A, B, C };

static class TypeConverter
{
public static string ToString(Bob myTypeX)
{
return string.Format("This is a type ({0})",
myTypeX.ToString());
}
}


class Program
{
static void Main(string[] args)
{
Bob aVariable = Bob.B;

System.Console.WriteLine(aVariable.ToString());

System.Console.WriteLine(TypeConverter.ToString
(aVariable));
}
}
}
 
S

shapper

namespace ConsoleApplication1
{
    enum Bob { A, B, C };

    static class TypeConverter
    {
        public static string ToString(Bob myTypeX)
        {
            return string.Format("This is a type ({0})",
myTypeX.ToString());
        }
    }
}

How would I integrate culture in the process?

So you advice me to create such a String Extension for each Enum type
I need to convert?

On my code I have one String extension and I implement TypeConverters
for each conversion I need.

It seems more correct ... but yes I will need to create more code.

I am still not sure the best way to do this.

I am Goggling ... :)
 
S

shapper

namespace ConsoleApplication1
{
    enum Bob { A, B, C };

    static class TypeConverter
    {
        public static string ToString(Bob myTypeX)
        {
            return string.Format("This is a type ({0})",
myTypeX.ToString());
        }
    }
}

How would I integrate culture in the process?

So you advice me to create such a String Extension for each Enum type
I need to convert?

On my code I have one String extension and I implement TypeConverters
for each conversion I need.

It seems more correct ... but yes I will need to create more code.

I am still not sure the best way to do this.

I am Goggling ... :)
 
M

Mihai N.

(e-mail address removed):

String aaa = MyEnum.AAA.ToStringType();

And aaa will become "This is AAA description".

This is working. My question is:
- Is this the best way to implement this?
- And is there a way to use the default ToString method?
(I am able to use String aaa = MyEnum.AAA.ToString() but I get
"AAA").

I have use Type converters to convert a List<T> to a CSV String. But
not enums.

Feels kind of complicated.

Why not use the string representation of the enum as a key for a
ResourceManager?
String theKey = MyEnum.AAA.ToString();
String value = resources.GetString( theKey );
// use value
 
M

Mihai N.

(e-mail address removed):

String aaa = MyEnum.AAA.ToStringType();

And aaa will become "This is AAA description".

This is working. My question is:
- Is this the best way to implement this?
- And is there a way to use the default ToString method?
(I am able to use String aaa = MyEnum.AAA.ToString() but I get
"AAA").

I have use Type converters to convert a List<T> to a CSV String. But
not enums.

Feels kind of complicated.

Why not use the string representation of the enum as a key for a
ResourceManager?
String theKey = MyEnum.AAA.ToString();
String value = resources.GetString( theKey );
// use value
 
P

Paul

This


Mihai N. said:
(e-mail address removed):



Feels kind of complicated.

Why not use the string representation of the enum as a key for a
ResourceManager?
String theKey = MyEnum.AAA.ToString();
String value = resources.GetString( theKey );
// use value
 
P

Paul

This


Mihai N. said:
(e-mail address removed):



Feels kind of complicated.

Why not use the string representation of the enum as a key for a
ResourceManager?
String theKey = MyEnum.AAA.ToString();
String value = resources.GetString( theKey );
// use value
 
J

Jeroen de Zeeuw

Hi,

Or you could decorate your enum members with attributes, containing the
string representation of your enum member, and create an extension method
like .ToDisplayName() or something. If you need to support multiple
languages, you could also use the attribute to hold the key of the string as
a label and let the extension method look it up in your resource file.

Hope this helps.

Jeroen de Zeeuw


Paul said:
 
J

Jeroen de Zeeuw

Hi,

Or you could decorate your enum members with attributes, containing the
string representation of your enum member, and create an extension method
like .ToDisplayName() or something. If you need to support multiple
languages, you could also use the attribute to hold the key of the string as
a label and let the extension method look it up in your resource file.

Hope this helps.

Jeroen de Zeeuw


Paul said:
 
S

shapper

Or you could decorate your enum members with attributes, containing the
string representation of your enum member, and create an extension method
like .ToDisplayName() or something. If you need to support multiple
languages, you could also use the attribute to hold the key of the stringas
a label and let the extension method look it up in your resource file.

I found that option to.
But by comparing all the options I really think I am going for a type
converter.

Is not the straight forward code but I think it will probably cover
all the different scenarios I have.
In relation to Resources I don't use the default XML files.
I created one for my own that uses SQL Server ... or also XML files.
But it is a little bit more flexible.

At the moment I have only one problem:
How to apply Type Converters in two directions.

I posted an example here:
http://groups.google.com/group/micr...csharp/browse_thread/thread/902a95a61e07017e#

It is my finished type converter but applied to a List.

My problem is two create the extension method so I can make the
conversion in two directions.

Does anyone knows the right way to do this?

Thanks,
Miguel
 
S

shapper

Or you could decorate your enum members with attributes, containing the
string representation of your enum member, and create an extension method
like .ToDisplayName() or something. If you need to support multiple
languages, you could also use the attribute to hold the key of the stringas
a label and let the extension method look it up in your resource file.

I found that option to.
But by comparing all the options I really think I am going for a type
converter.

Is not the straight forward code but I think it will probably cover
all the different scenarios I have.
In relation to Resources I don't use the default XML files.
I created one for my own that uses SQL Server ... or also XML files.
But it is a little bit more flexible.

At the moment I have only one problem:
How to apply Type Converters in two directions.

I posted an example here:
http://groups.google.com/group/micr...csharp/browse_thread/thread/902a95a61e07017e#

It is my finished type converter but applied to a List.

My problem is two create the extension method so I can make the
conversion in two directions.

Does anyone knows the right way to do this?

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top