473,832 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enumerator literal and passing strings

if i have a string eg

string day="monday";
and an enumerator

enum days {monday=0,tuesd ay, 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 1600
tony collier <me*****@hotmai l.com> wrote:
if i have a string eg

string day="monday";
and an enumerator

enum days {monday=0,tuesd ay, 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(type of(Days), day)

--
Jon Skeet - <sk***@pobox.co m>
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.co m

"tony collier" <me*****@hotmai l.com> wrote in message
news:Xn******** *************** ********@140.99 .99.130...
if i have a string eg

string day="monday";
and an enumerator

enum days {monday=0,tuesd ay, 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.co m> wrote in
news:MP******** *************** *@msnews.micros oft.com:
(Days) Enum.Parse(type of(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(type of(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*****@hotmai l.com> wrote:
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in
news:MP******** *************** *@msnews.micros oft.com:
(Days) Enum.Parse(type of(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(type of(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.co m>
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.co m> wrote in
news:MP******** *************** *@msnews.micros oft.com:
tony collier <me*****@hotmai l.com> wrote:
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in
news:MP******** *************** *@msnews.micros oft.com:
> (Days) Enum.Parse(type of(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(type of(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*****@hotmai l.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.WriteLi ne, 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.co m>
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.co m> wrote in
news:MP******** *************** *@msnews.micros oft.com:
tony collier <me*****@hotmai l.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.WriteLi ne, 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
17549
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 * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf( "%d\n", sizeof( x ) ); -Don
4
5401
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 literal. But the second printf seems to give me garbage. Any advise on what I am doing wrong? Thanx
1
1360
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 documenting...
20
5669
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 client-side HTML, CSS, etc. What I want to do is somehow insert a *server control* into the , then set the server control's properties at runtime.
2
2482
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 ArrayList to hold the com objects ought to work. If I add strings to the ArrayList and return the list, the JScript enumerator object works great to enumerate through all the strings but for some reason when I put com objects in the array list...
0
1754
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 below, I can take 5 characters and create a const uint64_t value at compile time. I can then create a switch statement that has these const values as the various cases. Since each case resolves to an integer constant value at compile time,
6
7480
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 you only need a few of them. On the other hand the iterator does not need to load more than 1 object hence uses less memory. But can't you do the same thing with an enumerator just by returning the next object when Current is called? If that's...
3
3641
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 from object to string frequently). Basically generics and not its non- generic counterpart. string str1 = "abc: value1 def: value2 ghi: value3"; char delimiterChars = { '\t' }; string tokens = str1.Split(delimiterChars);
33
2106
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 days to learn all those changes! I can see it fixes several of the missing things/problems I have found in Python in the past, like the lack of information regarding the floating point it uses, etc. I have seen that many (smart) updates are from...
0
9795
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10780
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10540
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5623
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.