473,394 Members | 1,829 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,394 software developers and data experts.

type casting with enumerations

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!
Nov 17 '05 #1
2 1081
How about Enum.Parse()?

"MrNobody" <Mr******@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
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!

Nov 17 '05 #2
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

Nov 17 '05 #3

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
4
by: ad | last post by:
I am confuse about type and class in some book about C#. What is the difference between type and class?
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
4
by: mark.olszowka | last post by:
I am writing a generic property table as part of my application property_name property_type property_value All information is stored in the database as strings. In my application that...
59
by: peter.tornqvist | last post by:
Maybe I'm stupid or maybe I am missing the obvious, but I can't find a way to define a type alias in C# for primitive types (class and interfaces I can inherit from, no problem). I.e I want to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.