type casting with enumerations

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

so let's say i have an array of strings like "Monday", "Tuesday",
"Wednesday", etc...

and I'm writing a parser that parses a day from a string and for memory
consevartion instead of saving the string I want to convert it into an
enumeration like:

enum Days : sbyte { MONDAY, TUESDAY, WEDNESDAY, etc ... }

So what's the best way to convert that "Monday" into a Days.MONDAY?

They are both at index 0 so I thought it would be easy but I can't figure it
out...

any help would be much appreciated!
 
Hi,

Enum.Parse has an option to ignore case. That is the best way if the
enum names and string values are equal ignoring case.
Otherwise:
They are both at index 0 so I thought it would be easy but I can't figure it
out...
If you can rely on index that way, use something similar to this:
string theDay = "Monday"; // or something else
Days theDay = (Days) Array.IndexOf(myStringArray, theDay);
(I omit the check Array.Index returns -1).

Otherwise, you then have to use "switch" statement.

Regards,
Thi
 

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