Enums

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

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?
 
when I try that, it then highlights my DbType.Parse nad give me the error
of Can not convert Object to System.Data.DbType
 
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
 
You need a typecast.

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

-vJ
 
Jim Heavey said:
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);
 
Back
Top