473,322 Members | 1,494 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,322 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 3211
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: 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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.