473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

enumerator literal and passing strings

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
Nov 15 '05 #1
7 1577
tony collier <me*****@hotmail.com> wrote:
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)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tony collier" <me*****@hotmail.com> wrote in message
news:Xn*******************************@140.99.99.1 30...
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

Nov 15 '05 #3
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
(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.
Nov 15 '05 #4
tony collier <me*****@hotmail.com> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
(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?


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
tony collier <me*****@hotmail.com> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
> (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?


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.
Nov 15 '05 #6
tony collier <me*****@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
tony collier <me*****@hotmail.com> wrote:
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.


OK - got it. Thanks
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
1
by: Jim Heavey | last post by:
In C# is there some sort of Enumerator which has "friendly" names for things like line feed, tabs, carriage control, etc.? I would perfer to use then using ascii values as I think this is more self...
20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
2
by: Jayme Pechan | last post by:
I migrated a formerly C++ COM component to C# and I have been able to duplicate all the old behavior except for one. I have an enumeration property that returns a list of com objects. Using the...
0
by: steve.lorimer | last post by:
Thank you for taking the time to look at this: I'm looking for a pre-processor command that will allow me to resolve const strings into const char literals at compile time. Looking at the code...
6
by: Michael C | last post by:
I'm reading about Iterators and the article I'm reading states that an enumerator must load all of the objects into memory, which is obviously a big waste if there are a large number of objects and...
3
by: Dave | last post by:
I'm calling string.Split() producing output string. I need direct access to its enumerator, but would greatly prefer an enumerator strings and not object types (as my parsing is unsafe casting...
33
by: bearophileHUGS | last post by:
I have just re-read the list of changes in Python 2.6, it's huge, there are tons of changes and improvements, I'm really impressed: http://docs.python.org/dev/whatsnew/2.6.html I'll need many...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.