Thread: Enum problem

  1. #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

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    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().

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Code:
    int value = 5;
    textBox1.Text = ((types)value).ToString(); // should be trailer

  5. #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

  6. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Problem with enum and files
    By Robert_Sitter in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2005, 01:40 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. enum code problem
    By correlcj in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 11:00 PM