473,398 Members | 2,165 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,398 software developers and data experts.

Enums

Trying to figure out how to convert back an forth between and Enum.

I am working with DbType. I am want to create a string value of what a
particular DbType is, say "DbType.String" to "String" and then convert
"string" back to "DbType.String"

I understand that I can get the string value as follows:

string myEnumString = DbType.String.ToString();

I also understand that I get turn it back into the "Enum" as follows:
DbType myEnumType = DbType.Parse(GetType(DbType), myEnumString)

This method does not compile - It highlights "DbType" and tells me it
expects a variable and not a class.

Any Idea what I am doing wrong?
Nov 15 '05 #1
5 3221
Instead of GetType(DbType), try typeof(DbType)

-vJ

"Jim Heavey" <Ji*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.16...
Trying to figure out how to convert back an forth between and Enum.

I am working with DbType. I am want to create a string value of what a
particular DbType is, say "DbType.String" to "String" and then convert
"string" back to "DbType.String"

I understand that I can get the string value as follows:

string myEnumString = DbType.String.ToString();

I also understand that I get turn it back into the "Enum" as follows:
DbType myEnumType = DbType.Parse(GetType(DbType), myEnumString)

This method does not compile - It highlights "DbType" and tells me it
expects a variable and not a class.

Any Idea what I am doing wrong?

Nov 15 '05 #2
when I try that, it then highlights my DbType.Parse nad give me the error
of Can not convert Object to System.Data.DbType

Nov 15 '05 #3
DbType myEnumType = DbType.Parse(typeof(DbType), myEnumString);

OR

DbType myEnumType = DbType.Parse(DbType.GetType(), myEnumString);
to my knowledge, there is no gen. purpose GetType function in c#
I believe, you might actually be doing GetType (in above line), which might be a call to a method of an enclosing type,
hence the error

Kalpesh

Nov 15 '05 #4
You need a typecast.

DbType myEnumType = (DbType) DbType.Parse(GetType(DbType), myEnumString)

-vJ
"Jim Heavey" <Ji*******@nospam.com> wrote in message
news:Xn********************************@207.46.248 .16...
when I try that, it then highlights my DbType.Parse nad give me the error
of Can not convert Object to System.Data.DbType

Nov 15 '05 #5
Jim Heavey <Ji*******@nospam.com> wrote:
when I try that, it then highlights my DbType.Parse nad give me the error
of Can not convert Object to System.Data.DbType


Full version:

DbType foo = (DbType) Enum.Parse (typeof(DbType), myEnumString);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

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

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
2
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
2
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
11
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course,...
4
by: Jon Slaughter | last post by:
is there a simple way to "step" through enums? I have a button that I want to click and have it "cycle" through a set of states defined by enums but the only way I can think of doing this...
13
by: Bob | last post by:
Hi, Can someone explain why you can't declare enums in an interface? The compiler says "interfaces can't declare types" Ignoring the syntax implications it seems to me that you should be able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.