I have a custom enum to list comparison operators: =, <, <=, >, >= . I have
created it as follows:
public enum Comparator {Equal, LessThan,
LessThanOrEqual ,GreaterThan,Gr eaterThanOrEqua l}
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 5 4031
"John Smith" <JS****@Phoney. com> wrote in
news:e6******** ******@TK2MSFTN GP12.phx.gbl: I have a custom enum to list comparison operators: =, <, <=, >,= . I have created it as follows:
public enum Comparator {Equal, LessThan, LessThanOrEqual ,GreaterThan,Gr eaterThanOrEqua l}
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.Collecti ons;
namespace Main
{
public class MainClass
{
public enum Comparator
{
Equal,
LessThan,
LessThanOrEqual ,
GreaterThan,
GreaterThanOrEq ual
}
private static Hashtable comparators = new Hashtable();
[STAThread]
public static void Main(string[] args)
{
comparators.Add (Comparator.Equ al, "=");
comparators.Add (Comparator.Les sThan, "<");
comparators.Add (Comparator.Les sThanOrEqual, "<=");
comparators.Add (Comparator.Gre aterThan, ">");
comparators.Add (Comparator.Gre aterThanOrEqual , ">=");
comparators.Add ("=", Comparator.Equa l);
comparators.Add ("<", Comparator.Less Than);
comparators.Add ("<=", Comparator.Less ThanOrEqual);
comparators.Add (">", Comparator.Grea terThan);
comparators.Add (">=", Comparator.Grea terThanOrEqual) ;
Console.WriteLi ne("The Comparator.Less ThanOrEqual sign is {0}",
comparators[Comparator.Less ThanOrEqual].ToString());
Console.WriteLi ne("The name of the Comparator enum for " +
"the > sign is {0}",
Enum.GetName(ty peof(Comparator ), (int) comparators[">"]));
}
}
}
Hope this helps.
Chris.
-------------
C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/
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
"John Smith" <JS****@Phoney. com> wrote in message
news:e6******** ******@TK2MSFTN GP12.phx.gbl... I have a custom enum to list comparison operators: =, <, <=, >, >= . I
have created it as follows:
public enum Comparator {Equal, LessThan, LessThanOrEqual ,GreaterThan,Gr eaterThanOrEqua l}
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
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( AttributeTarget s.All)]
public class LabelAttribute : Attribute
{
public readonly string Label;
public LabelAttribute( string label)
{
Label = label;
}
public static string FromMember(obje ct o)
{
return ((LabelAttribut e)
o.GetType().Get Member(o.ToStri ng())[0].GetCustomAttri butes(typeof(La belAttri
bute), false)[0]).Label;
}
public static string FromType(object o)
{
return ((LabelAttribut e)
o.GetType().Get CustomAttribute s(typeof(LabelA ttribute), false)[0]).Label;
}
}
Then you can use this attribute class to associate a string label with each
of your enum values:
[Label("Comparis on operators")]
public enum Comparator
{
[Label("=")]
Equal,
[Label("<")]
LessThan,
[Label("<=")]
LessThanOrEqual ,
[Label(">")]
GreaterThan,
[Label(">=")]
GreaterThanOrEq ual,
}
And this is how you would read those labels:
Comparator x = Comparator.Grea terThanOrEqual;
string valueLabel = LabelAttribute. FromMember(x);
string typeLabel = LabelAttribute. FromType(x);
"John Smith" <JS****@Phoney. com> wrote in message
news:e6******** ******@TK2MSFTN GP12.phx.gbl... I have a custom enum to list comparison operators: =, <, <=, >, >= . I
have created it as follows:
public enum Comparator {Equal, LessThan, LessThanOrEqual ,GreaterThan,Gr eaterThanOrEqua l}
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
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( AttributeTarget s.All)]
public class LabelAttribute : Attribute
{
public readonly string Label;
public LabelAttribute( string label)
{
Label = label;
}
public static string FromMember(obje ct o)
{
return ((LabelAttribut e)
o.GetType().Get Member(o.ToStri ng())[0].GetCustomAttri butes(typeof(La belAttri
bute), false)[0]).Label;
}
public static string FromType(object o)
{
return ((LabelAttribut e)
o.GetType().Get CustomAttribute s(typeof(LabelA ttribute), false)[0]).Label;
}
}
Then you can use this attribute class to associate a string label with each
of your enum values:
[Label("Comparis on operators")]
public enum Comparator
{
[Label("=")]
Equal,
[Label("<")]
LessThan,
[Label("<=")]
LessThanOrEqual ,
[Label(">")]
GreaterThan,
[Label(">=")]
GreaterThanOrEq ual,
}
And this is how you would read those labels:
Comparator x = Comparator.Grea terThanOrEqual;
string valueLabel = LabelAttribute. FromMember(x);
string typeLabel = LabelAttribute. FromType(x);
"John Smith" <JS****@Phoney. com> wrote in message
news:e6******** ******@TK2MSFTN GP12.phx.gbl... I have a custom enum to list comparison operators: =, <, <=, >, >= . I
have created it as follows:
public enum Comparator {Equal, LessThan, LessThanOrEqual ,GreaterThan,Gr eaterThanOrEqua l}
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
You get the prize, Bret! Thanks to Nicole and Chris too, for their excellent
suggestions.
"Bret Mulvey" <br***@microsof t.nospam0000.co m> wrote in message
news:hf******** ************@rw crnsc51.ops.asp .att.net... 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( AttributeTarget s.All)] public class LabelAttribute : Attribute { public readonly string Label;
public LabelAttribute( string label) { Label = label; }
public static string FromMember(obje ct o) { return ((LabelAttribut e)
o.GetType().Get Member(o.ToStri ng())[0].GetCustomAttri butes(typeof(La belAttri bute), false)[0]).Label; }
public static string FromType(object o) { return ((LabelAttribut e) o.GetType().Get CustomAttribute s(typeof(LabelA ttribute), false)[0]).Label; } }
Then you can use this attribute class to associate a string label with
each of your enum values:
[Label("Comparis on operators")] public enum Comparator { [Label("=")] Equal,
[Label("<")] LessThan,
[Label("<=")] LessThanOrEqual ,
[Label(">")] GreaterThan,
[Label(">=")] GreaterThanOrEq ual, }
And this is how you would read those labels:
Comparator x = Comparator.Grea terThanOrEqual; string valueLabel = LabelAttribute. FromMember(x); string typeLabel = LabelAttribute. FromType(x);
"John Smith" <JS****@Phoney. com> wrote in message news:e6******** ******@TK2MSFTN GP12.phx.gbl... I have a custom enum to list comparison operators: =, <, <=, >, >= . I have created it as follows:
public enum Comparator {Equal, LessThan, LessThanOrEqual ,GreaterThan,Gr eaterThanOrEqua l}
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Lex |
last post by:
I am writing a C# app that has a Menu. Some of the menu items will
have short cuts that do not exist in the Shortcut enum. I would like
the custom shortcuts to appear on the menu but as far as I can tell
there is no way to add a short cut that is not in the Shortcut enum.
I have my on keyboard hook so I am not worried about the custom
shortcut magically working. I just want it to appear nicely formated
on the menu.
Anyone have any...
|
by: Wiktor Zychla |
last post by:
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'
|
by: Jon Turner |
last post by:
When developing a custom control, lets say I have a property named
"WidgetType" and
this property can consist of 3 types "WidgetStandard, WidgetEnhanced,
WidgetSimple".
How does my control notify the IDE that these are the types of WidgetType so
that the
IDE can display a Listbox with these types ? Does this have to be an enum ?
Many Thanks In Advance.
|
by: Ed Bick |
last post by:
I built a custom control. It has one custom property that I created
exposed. What I want to do though is to have the property show as a
dropdown with only a limited number of choices for settings. An example
would be the dropdown style of a combo box. The designer can only select
one of three previously defined options and can not type anything arbitrary
in.
Can anyone explain how to do that?
|
by: Jamey Shuemaker |
last post by:
I'm in the process of expanding my knowledge and use of Class Modules.
I've perused MSDN and this and other sites, and I'm pretty comfortable
with my understanding of Class Modules with the exception of custom
Collection Classes.
Background: I'm developing an A2K .mdb to be deployed as an .mde at my
current job-site. It has several custom controls which utilize custom
classes to wrap built-in controls, and add additional functionality....
| |
by: Jeff Mason |
last post by:
Hi,
I'm having a reflection brain fog here, perhaps someone can set me on the right
track.
I'd like to define a custom attribute to be used in a class hierarchy.
What I want to do is to have an attribute which can be applied to a class definition
of a class which inherits from a base, mustinherit class. I want to define methods
in the base class which will access the contents of the attribute as it is applied to
|
by: filipk69 |
last post by:
Could someone point me in the right direction, please.
I have an enum something like this:
enum TextFieldType
{
FieldABC = 3000,
FieldXYZ = 3001,
…..
…..
}
|
by: Rick |
last post by:
Hi group,
I'm trying to inherit from the windows checkbox control. This is no
problem. My goal is to add a property that is based on an enum. When I
have an enum declared within the class, it works correctly, however,
my enum is declared in a referenced assembly. The form cannot load the
enum from the referenced assembly, it indicates that it can't load the
type.
using VB, VS2005.
|
by: Rex the Strange |
last post by:
Hello all,
Can anyone please tell me why I can't do this (and what I can do to
get the equivalent effect):
In the code:
public enum myenum
value1
value2
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |