473,387 Members | 1,569 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,387 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 3216
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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,...

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.