enumerator literal and passing strings

  • Thread starter Thread starter tony collier
  • Start date Start date
T

tony collier

if i have a string eg

string day="monday";


and an enumerator

enum days {monday=0,tuesday, wednesday}


how can i use the enumerator so that instead of writing

days.monday

to return 0

i can use the value of the string day as the enum literal eg. something
like:


days."value of day"

so that if day="monday" this equals 0, if day="tuesday" this equals 1 and
if day="wednesday" this returns 2
 
tony collier said:
if i have a string eg

string day="monday";


and an enumerator

enum days {monday=0,tuesday, wednesday}


how can i use the enumerator so that instead of writing

days.monday

to return 0

i can use the value of the string day as the enum literal eg. something
like:

days."value of day"

so that if day="monday" this equals 0, if day="tuesday" this equals 1 and
if day="wednesday" this returns 2

I believe you're after:

(Days) Enum.Parse(typeof(Days), day)
 
Tony,

In order to do that, you can call the static Parse method on the Enum
class, passing the type of the enumeration, and the name of the value you
want to get.

Hope this helps.
 
(Days) Enum.Parse(typeof(Days), day)

thanks alot for getting me on the right track. I actually wanted the
integer that the element represented so i used this instead:

(int) Enum.Parse(typeof(Days), day)


I have now done a bit of reading around and am slightly confused. I have
read this on one site:

"If we would have omitted the cast, the output would be the enum member,
which would then be converted to a string representation of the member
name"


As i understand the above statement is saying that in this case i would be
returned whatever string is stored in day. However when i try omitting
(int) above i am returned an object which can't be implicitly cast. Could
you clear this up for me?

thanks for all your help.
 
tony collier said:
thanks alot for getting me on the right track. I actually wanted the
integer that the element represented so i used this instead:

(int) Enum.Parse(typeof(Days), day)


I have now done a bit of reading around and am slightly confused. I have
read this on one site:

"If we would have omitted the cast, the output would be the enum member,
which would then be converted to a string representation of the member
name"

As i understand the above statement is saying that in this case i would be
returned whatever string is stored in day. However when i try omitting
(int) above i am returned an object which can't be implicitly cast. Could
you clear this up for me?

If you omit the cast, the return type is just Enum. I'm not sure where
the "would then be converted to a string representation of the member
name" came from, but that would depend entirely on the context.
 
If you omit the cast, the return type is just Enum. I'm not sure where
the "would then be converted to a string representation of the member
name" came from, but that would depend entirely on the context.

I thought that the default base type of enum was int. So unless :byte ,
:short etc, is specified in enum declaration. isn't the return type int
? if you have time please take a look at
http://www.csharp-station.com/Tutorials/Lesson17.pdf

this is where i am getting all my info from.

thanks.
 
tony collier said:
I thought that the default base type of enum was int. So unless :byte ,
:short etc, is specified in enum declaration. isn't the return type int
?

No. The return type *must* be Enum, as defined by Enum.Parse. The
actual base type of the enum parameter isn't (necessarily) known at
compile time.
if you have time please take a look at
http://www.csharp-station.com/Tutorials/Lesson17.pdf

this is where i am getting all my info from.

Right - in this case, because the value is being used as a parameter to
Console.WriteLine, it would be passed as object (from Enum) and then
ToString would be called on the enum value. The string conversion has
nothing to do with the actual return type or value of Enum.Parse.
 
No. The return type *must* be Enum, as defined by Enum.Parse. The
actual base type of the enum parameter isn't (necessarily) known at
compile time.


Right - in this case, because the value is being used as a parameter
to Console.WriteLine, it would be passed as object (from Enum) and
then ToString would be called on the enum value. The string conversion
has nothing to do with the actual return type or value of Enum.Parse.

OK - got it. Thanks
 
Back
Top