Custom enum poser

J

John Smith

I have a custom enum to list comparison operators: =, <, <=, >, >= . I have
created it as follows:

public enum Comparator {Equal, LessThan,
LessThanOrEqual,GreaterThan,GreaterThanOrEqual}

Now I have to create static functions to convert these enum values to/from
the actual comparison operator strings. As I understand it, I can't add
methods to an enum, so is there any more elegant way to do this?

TIA
 
C

Chris R. Timmons

public enum Comparator {Equal, LessThan,
LessThanOrEqual,GreaterThan,GreaterThanOrEqual}

Now I have to create static functions to convert these enum
values to/from the actual comparison operator strings. As I
understand it, I can't add methods to an enum, so is there any
more elegant way to do this?

John,

One solution would be to use a Hashtable:


using System;
using System.Collections;

namespace Main
{
public class MainClass
{
public enum Comparator
{
Equal,
LessThan,
LessThanOrEqual,
GreaterThan,
GreaterThanOrEqual
}

private static Hashtable comparators = new Hashtable();

[STAThread]
public static void Main(string[] args)
{
comparators.Add(Comparator.Equal, "=");
comparators.Add(Comparator.LessThan, "<");
comparators.Add(Comparator.LessThanOrEqual, "<=");
comparators.Add(Comparator.GreaterThan, ">");
comparators.Add(Comparator.GreaterThanOrEqual, ">=");
comparators.Add("=", Comparator.Equal);
comparators.Add("<", Comparator.LessThan);
comparators.Add("<=", Comparator.LessThanOrEqual);
comparators.Add(">", Comparator.GreaterThan);
comparators.Add(">=", Comparator.GreaterThanOrEqual);

Console.WriteLine("The Comparator.LessThanOrEqual sign is {0}",
comparators[Comparator.LessThanOrEqual].ToString());
Console.WriteLine("The name of the Comparator enum for " +
"the > sign is {0}",
Enum.GetName(typeof(Comparator), (int) comparators[">"]));
}
}
}



Hope this helps.

Chris.
 
N

Nicole Calinoiu

Create a class that contains both the enum and a method (probably static) to
map an particular value to the operator string. The enum could, of course,
be separate from the class that contains the method, but the encapsulation
wouldn't be quite as clean.

HTH,
Nicole
 
B

Bret Mulvey

A very .NET-ish sort of way would be to use attributes. First, create an
attribute class that can be used to assign a string label to something, such
as an enum value. This is more code than you need for your specific case,
but this is a very general class that can be used to assign a string label
to pretty much anything, for any purpose. Very reusable.


using System;

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

public LabelAttribute(string label)
{
Label = label;
}

public static string FromMember(object o)
{
return ((LabelAttribute)
o.GetType().GetMember(o.ToString())[0].GetCustomAttributes(typeof(LabelAttri
bute), false)[0]).Label;
}

public static string FromType(object o)
{
return ((LabelAttribute)
o.GetType().GetCustomAttributes(typeof(LabelAttribute), false)[0]).Label;
}
}


Then you can use this attribute class to associate a string label with each
of your enum values:


[Label("Comparison operators")]
public enum Comparator
{
[Label("=")]
Equal,

[Label("<")]
LessThan,

[Label("<=")]
LessThanOrEqual,

[Label(">")]
GreaterThan,

[Label(">=")]
GreaterThanOrEqual,
}


And this is how you would read those labels:


Comparator x = Comparator.GreaterThanOrEqual;
string valueLabel = LabelAttribute.FromMember(x);
string typeLabel = LabelAttribute.FromType(x);
 
J

John Smith

You get the prize, Bret! Thanks to Nicole and Chris too, for their excellent
suggestions.

Bret Mulvey said:
A very .NET-ish sort of way would be to use attributes. First, create an
attribute class that can be used to assign a string label to something, such
as an enum value. This is more code than you need for your specific case,
but this is a very general class that can be used to assign a string label
to pretty much anything, for any purpose. Very reusable.


using System;

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

public LabelAttribute(string label)
{
Label = label;
}

public static string FromMember(object o)
{
return ((LabelAttribute)
o.GetType().GetMember(o.ToString())[0].GetCustomAttributes(typeof(LabelAttri
bute), false)[0]).Label;
}

public static string FromType(object o)
{
return ((LabelAttribute)
o.GetType().GetCustomAttributes(typeof(LabelAttribute), false)[0]).Label;
}
}


Then you can use this attribute class to associate a string label with each
of your enum values:


[Label("Comparison operators")]
public enum Comparator
{
[Label("=")]
Equal,

[Label("<")]
LessThan,

[Label("<=")]
LessThanOrEqual,

[Label(">")]
GreaterThan,

[Label(">=")]
GreaterThanOrEqual,
}


And this is how you would read those labels:


Comparator x = Comparator.GreaterThanOrEqual;
string valueLabel = LabelAttribute.FromMember(x);
string typeLabel = LabelAttribute.FromType(x);




John Smith said:
I have a custom enum to list comparison operators: =, <, <=, >, >= . I have
created it as follows:

public enum Comparator {Equal, LessThan,
LessThanOrEqual,GreaterThan,GreaterThanOrEqual}

Now I have to create static functions to convert these enum values to/from
the actual comparison operator strings. As I understand it, I can't add
methods to an enum, so is there any more elegant way to do this?

TIA
 

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

Similar Threads

Enum TypeConverter 3
about enum 3
Enum Extentions 7
Enums and Text Descriptions 2
Going from string to enum? 2
finding an Enum using the string name of it 4
Deserialization of invalid enum values 3
Nullable Enum 2

Top