Fabrice's weblog

Tools and Source

News

My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET

transatlantys hot news

Contact

Me

Others

Selected content

C# feature request for Anders

Hello Anders,

Could you add to C# support for things like this?

enum Colors {Red, Green, Blue};
string[Colors] htmlColors = new string[] {"#FF000", "#00FF00", "#0000FF"};

This is something we have in Delphi (you remember Delphi, don't you?) that I miss in C#. I don't think it would be difficult to add to the compiler. But if we don't have it yet, maybe there is a problem somewhere?

Of course, we can find more examples where this syntax would help, and not only with strings.
If we go a bit further, we can even imagine something like this:

enum Colors<string> {Red = "#FF0000", Green = "#00FF00", Blue = "#0000FF"};

Let me know what you think,
Yours truly,
Fabrice

Comments

TrackBack said:

# June 24, 2004 7:16 AM

TrackBack said:

# June 24, 2004 7:37 AM

Uwe said:

So you have an associative array (dictionary) where the keys are of type 'Color' and the values are of type 'string'?.

You could do that with Generics, I guess...!?
# June 24, 2004 9:04 AM

Fabrice said:

This is getting close, but the advantage with enums is that the set of possible keys is well defined and compile-time cheched.
# June 24, 2004 9:33 AM

Chris Martin said:

What I do for this is use a custom attribute that I call "MemberDescriptionAttribute". Then I can get it from reflection.

[AttributeUsage(AttributeTargets.All)]
public class MemberDescriptionAttribute : Attribute
{
public readonly string Description;

public MemberDescriptionAttribute(string description)
{
this.Description = description;
}

public static string FromMember(object obj)
{
return((MemberDescriptionAttribute)obj.GetType().GetMember(
obj.ToString())[0].GetCustomAttributes(typeof(MemberDescriptionAttribute), false)[0]).Description;
}

public static string FromType(object obj)
{
return((MemberDescriptionAttribute)obj.GetType().GetCustomAttributes(typeof(MemberDescriptionAttribute), false)[0]).Description;
}

public static ArrayList ToArrayList(System.Type type)
{
string[] names = Enum.GetNames(type);
ArrayList toReturn = new ArrayList( names.Length );
DictionaryEntry de;
foreach(string n in names)
{
object current = Enum.Parse(type, n, true);
string description = MemberDescriptionAttribute.FromMember(current);
de = new DictionaryEntry();
de.Key = description;
de.Value = n;
toReturn.Add( de );
}

return toReturn;
}
}

class Class1
{
public enum Colors
{
[MemberDescription("#FF000")]
Red,

[MemberDescription("#00FF00")]
Green,

[MemberDescription("#0000FF")]
Blue
}

[STAThread]
static void Main(string[] args)
{
ArrayList list = MemberDescriptionAttribute.ToArrayList(typeof(Colors));

foreach(DictionaryEntry de in list)
{
Console.WriteLine(de.Value + " = " + de.Key);
}

Console.ReadLine();
}
}
# June 24, 2004 12:56 PM

Fabrice said:

Chris, I have something similar. See my post about the StringValueAttribute: http://weblogs.asp.net/fmarguerie/archive/2004/06/24/164902.aspx
# June 24, 2004 1:21 PM

TrackBack said:

# June 26, 2004 2:55 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)