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
 
Back
Top