C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-29-2009, 07:41 AM   #1
Registered User
 
Join Date: Oct 2008
Posts: 85
Enum problem

just a quickie - my head seems to have gone blank

i have an enum

Code:
public class1
{
public enum types
{
  car=1,
  bike =3,
  trailer=5,
  lorry = 11
}
}
i have a value ( say 5) - can i get the string ("trailer") into a text box this way ?

Code:
int value = 5;
textbox1.text = ????   <----- get the word 'trailer' to go to text box please
am i doing this the right way round or is an enum not the way i need to do it.

would i be better with a switch/case statement ?

thanks
deviousdexter is offline   Reply With Quote
Old 04-29-2009, 08:25 AM   #2
Registered User
 
valaris's Avatar
 
Join Date: Jun 2008
Location: RING 0
Posts: 468
You should use a hashtable if you want to do something like that (dictionary in C#). Otherwise just make an enum of type types (in your case), assign it one of its respective enum values, and then call toString().
valaris is offline   Reply With Quote
Old 04-29-2009, 08:36 AM   #3
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
Code:
enum MyEnum { One, Two, Three }
System.Enum.GetName(typeof(MyEnum), MyEnum.Two);
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 04-29-2009, 10:34 AM   #4
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 95
Code:
int value = 5;
textBox1.Text = ((types)value).ToString(); // should be trailer
theoobe is offline   Reply With Quote
Old 05-05-2009, 02:36 AM   #5
Registered User
 
Join Date: May 2009
Posts: 1
Code:
static partial class Extenstions
{
     static public string GetString(this Enum p_enum) {
          return Enum.GetName(p_enum.GetType(), p_enum);
     }
}
Code:
Numbers k = Numbers.One;
Console.WriteLine(k.GetString()); //One
fairweather is offline   Reply With Quote
Old 05-06-2009, 07:46 AM   #6
Registered User
 
Join Date: Oct 2008
Posts: 85
Sorry i thought i had posted back to this thread, must have forgotten the submit button.

both the Theoobe and Magos' solutions were tried and did what i needed - i opted for Magos solution for no particular reason.

Sorry for leaving the thread in flux.
deviousdexter is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
WS_POPUP, continuation of old problem blurrymadness Windows Programming 1 04-20-2007 06:54 PM
Laptop Problem Boomba Tech Board 1 03-07-2006 06:24 PM
Problem with enum and files Robert_Sitter C++ Programming 1 11-24-2005 01:40 AM
Sorting problem.. well actually more of a string problem fatdunky C Programming 5 11-07-2005 11:34 PM
enum code problem correlcj C++ Programming 4 11-15-2002 11:00 PM


All times are GMT -6. The time now is 10:19 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22