473,888 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting Enum Reference

Say that I create a combobox with pull down items that map directly to an
enum. For example the dropdown items could be "Up", "Down", "Left" and
"Right" and the enum could be declare as: public enum DirectionEnum {Up,
Down, Left, Right};

If I needed to get an enum reference based on the dropdown selection, as of
today I would do the following:

DirectionEnum myEnum; // Declare enum.
If(someCombobox .Text == "Up") myEnum == DirectionEnum.U p;
If(someCombobox .Text == "Down") myEnum == DirectionEnum.D own;
etc...

What I would like to do is something like my make believe following code:
myEnum = DirectionEnum.G etEnumByName(so meCombobox.Text );

Is there any way to achieve something like the code above?

Thank you.
Nov 15 '05 #1
2 1616
Rene <no****@nospam. nospam> wrote:
Say that I create a combobox with pull down items that map directly to an
enum. For example the dropdown items could be "Up", "Down", "Left" and
"Right" and the enum could be declare as: public enum DirectionEnum {Up,
Down, Left, Right};

If I needed to get an enum reference based on the dropdown selection, as of
today I would do the following:

DirectionEnum myEnum; // Declare enum.
If(someCombobox .Text == "Up") myEnum == DirectionEnum.U p;
If(someCombobox .Text == "Down") myEnum == DirectionEnum.D own;
etc...

What I would like to do is something like my make believe following code:
myEnum = DirectionEnum.G etEnumByName(so meCombobox.Text );

Is there any way to achieve something like the code above?


I think you're looking for:

myEnum = (DirectionEnum) Enum.Parse (typeof(Directi onEnum),
someCombobox.Te xt);

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Excellent!!

Thanks.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Rene <no****@nospam. nospam> wrote:
Say that I create a combobox with pull down items that map directly to an enum. For example the dropdown items could be "Up", "Down", "Left" and
"Right" and the enum could be declare as: public enum DirectionEnum {Up,
Down, Left, Right};

If I needed to get an enum reference based on the dropdown selection, as of today I would do the following:

DirectionEnum myEnum; // Declare enum.
If(someCombobox .Text == "Up") myEnum == DirectionEnum.U p;
If(someCombobox .Text == "Down") myEnum == DirectionEnum.D own;
etc...

What I would like to do is something like my make believe following code: myEnum = DirectionEnum.G etEnumByName(so meCombobox.Text );

Is there any way to achieve something like the code above?


I think you're looking for:

myEnum = (DirectionEnum) Enum.Parse (typeof(Directi onEnum),
someCombobox.Te xt);

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
4872
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
31
3644
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
3
3913
by: K. Wilder | last post by:
I need to declare a project level Enum that any procedure in any class can reference so there's continuity with the values. How do I do this? At present, if I declare a Module in a project and want to refer to it in an class I get the following error: cannot expose type outside the project through class
34
11227
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
6
35003
by: Rico | last post by:
Hello, I'm looking for a way to reference the string name of an enumerator. I know in VB.net you can do "MyEnum.ToString" and get the name instead of the integer value. Is there a way I can do something similar with an Enum created in Access? Alright, here is some air code to explain what I mean Public Enum MyEnum
3
12277
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: public enum Days { Sunday };
12
22782
by: Charlie | last post by:
I have a file, data.c, where I define all of my global variables. I then have a header file, data.h, which I include in every file in which I reference all of the variables defined in data.c. For the most part, I am having no problem with this. However, I defined an variable of type myenum in data.c: enum myenum {
3
1468
by: Adrian | last post by:
In the following code example I am trying to create a generic interface for a bunch of objects. Each concrete type is stored in its own container and the a container of pointer's to base is used so I can access all at once. I created a specialization of the template for Fruit *'s so that it returns a reference and the code looks the same as when accessing a concrete type. I've managed to get everything working so far except accessing all...
5
7536
by: Sin Jeong-hun | last post by:
Hello. Speical folders, like Desktop, usually have different display names. For example, in Japanese Windows, it's displayed as "デスクトップ". I want to get the this displayed name of a special folder but I can't find a suitable method. Environment.GetFolderPath() is to get the path not the name. There's no way provided the .NET BCL? I also tried to get it through Win32 API. I looked up the MSDN site...
0
9961
marktang
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
11178
Oralloy
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...
0
10777
jinu1996
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...
0
10438
tracyyun
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...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7148
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();...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4243
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.