2 Replies - 2911 Views - Last Post: 10 January 2008 - 06:04 PM Rate Topic: -----

#1 kayaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-October 07

associating alternative values to enum elements

Posted 10 January 2008 - 04:52 PM

if i have the following enum declaration:

		public enum DatePart
		{
			YEAR = 0,
			MONTH = 1,
			DAY = 2,
			HOUR = 3,
			MIN = 4,
			SEC = 5,
			MILLI = 6
		};



and i am referencing, say DatePart.MONTH in the code somewhere (outside the enum),

HOW DO I GET THE ASSOCIATED VALUE (1) ?

e.g.

Console.Write("Value = " + DatePart.MONTH);



prints

Value = MONTH

and i want

Value = 1

thx,

- a -

Is This A Good Question/Topic? 0
  • +

Replies To: associating alternative values to enum elements

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: associating alternative values to enum elements

Posted 10 January 2008 - 05:36 PM

Here you go...

// Set blah to the value of Month in the Enum... blah would then equal the value 1.
int blah = (int)Enum.Parse(typeof(DatePart),"MONTH")



This says parse the enum DatePart, look for the value with the name "MONTH" and cast its value to an integer.

Enjoy!

"At DIC we torture enums until they spill all their values.... we are that kind of code ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 kayaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-October 07

Re: associating alternative values to enum elements

Posted 10 January 2008 - 06:04 PM

thanks !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1