Return month from dropdown as integer

  • Thread starter Thread starter Bishman
  • Start date Start date
B

Bishman

Hi,

I am new to C# so forgive my ignorance.

I need to write a month 'number' to a DB, ie January = 1, March=3 etc etc...

I have the month description in a combo box. I used an enum " enum
month{January = 1,February, March....... etc } " to load the combo.

I wanted to use the same to resolve the String retrieved form the combo into
the actual enum value.

How would I do this ??

I attempted to do an 'Array.binarysearch' having populated an array with the
months of the year. That didnt work either.

Any help appreciated.

Jon.
 
Bishman,

It depends how you populate the combobox.
Example
enum month{January = 1,February, March....... etc }

you can populate the combobox
combobox.Items.Add(month.January);
combobox.Items.Add(month.February);
etc.

you can do: (int)combobox.SelectedItem;

If you populate the combobox with the names of the enumeration items you can
go for

(int)Enum.Parse(typeof(Months), str)
 
Hi Stoitcho,

Many thanks, the line " (int)Enum.Parse(typeof(Months), str) " is the
one I wanted, works fine.

I had a play around with Enum.Parse today also but just couldnt get it to
play ball.

Many thanks, Much appreciated.

Bish.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top