473,387 Members | 1,925 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.

Converting String to Enum Value

In a posting earlier this year I found a simple approach to convert a string
to a particular Enum value. The one line solution looked like this:

MyEnum ConvertedString = (MyEnum) Enum.Parse(typeof(MyEnum), MyString, true);

This is fine if one wants to hardcode each and every Enum in an If-ElseIf or
Select-Case construct but I'm wondering if there's a way to do it generically?

I have a situation where I'm using reflection to set a value. Here is the
code I've successfully been using:
propInfo.SetValue(node.Obj, Convert.ChangeType(newval,
propInfo.PropertyType, null), indexer);

This has worked perfectly for the basic user types (ex. string, bool, int,
etc.) but fails if I try to introduce a property that is defined with a type
of one of my Enums.

Any ideas?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #1
2 6773
Just a shot in the dark here...

Since you have propInfo.PropertyType, which is a System.Type, you should be
able to check System.Type.IsEnum to see if the type in question is an
enumeration. Then, you may be able to parse based on that. Something to
the effect of (note: I didn't test this):

if(propInfo.PropertyType.IsEnum)
{
object val = Enum.Parse(propInfo.PropertyType, newVal);
propInfo.SetValue(node.Obj, val, indexer);
}
//Maybe need some else if's for different property types??
else
{
//your existing code:
propInfo.SetValue(node.Obj, Convert.ChangeType(newval,
propInfo.PropertyType, null), indexer);

}
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
In a posting earlier this year I found a simple approach to convert a
string
to a particular Enum value. The one line solution looked like this:

MyEnum ConvertedString = (MyEnum) Enum.Parse(typeof(MyEnum), MyString,
true);

This is fine if one wants to hardcode each and every Enum in an If-ElseIf
or
Select-Case construct but I'm wondering if there's a way to do it
generically?

I have a situation where I'm using reflection to set a value. Here is the
code I've successfully been using:
propInfo.SetValue(node.Obj, Convert.ChangeType(newval,
propInfo.PropertyType, null), indexer);

This has worked perfectly for the basic user types (ex. string, bool, int,
etc.) but fails if I try to introduce a property that is defined with a
type
of one of my Enums.

Any ideas?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #2
Derrick,

Your shot in the dark was a good one! In addition to your change, I had to
enhance some code elsewhere but it was worth it. For now I am storing the
actual English enum values rather than their numeric equivalents. This
removes a layer of obfuscation, which will make troubleshooting and future
analysis much easier.

Thank you!

--
Robert W.
Vancouver, BC
www.mwtech.com

"Derrick" wrote:
Just a shot in the dark here...

Since you have propInfo.PropertyType, which is a System.Type, you should be
able to check System.Type.IsEnum to see if the type in question is an
enumeration. Then, you may be able to parse based on that. Something to
the effect of (note: I didn't test this):

if(propInfo.PropertyType.IsEnum)
{
object val = Enum.Parse(propInfo.PropertyType, newVal);
propInfo.SetValue(node.Obj, val, indexer);
}
//Maybe need some else if's for different property types??
else
{
//your existing code:
propInfo.SetValue(node.Obj, Convert.ChangeType(newval,
propInfo.PropertyType, null), indexer);

}
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
In a posting earlier this year I found a simple approach to convert a
string
to a particular Enum value. The one line solution looked like this:

MyEnum ConvertedString = (MyEnum) Enum.Parse(typeof(MyEnum), MyString,
true);

This is fine if one wants to hardcode each and every Enum in an If-ElseIf
or
Select-Case construct but I'm wondering if there's a way to do it
generically?

I have a situation where I'm using reflection to set a value. Here is the
code I've successfully been using:
propInfo.SetValue(node.Obj, Convert.ChangeType(newval,
propInfo.PropertyType, null), indexer);

This has worked perfectly for the basic user types (ex. string, bool, int,
etc.) but fails if I try to introduce a property that is defined with a
type
of one of my Enums.

Any ideas?

--
Robert W.
Vancouver, BC
www.mwtech.com


Nov 17 '05 #3

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

Similar topics

2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
5
by: gmccallum | last post by:
I am trying to convert the value of a string to a defined enum value such as follows. public enum MyEnum { One, Two }; string MyString = "One"; // or even this is fine string MyString2 =...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
2
by: Richy | last post by:
Hi, I have a class that exposes the Microsoft.DirectX.Direct3D.Compare property, which allows a drop-down list of values (such as Never, Always etc) to be selected via the propertygrid. I write...
8
by: tony | last post by:
Hello! I have below a for loop and a switch in the for loop. I have also a enum called colBlowStep with some values. I have also an array called m_columnBlowStep with some strings. All items in...
6
by: Rico | last post by:
Hello, I'm looking for a way to reference the string name of an enumerator. I know in VB.net you can do "MyEnum.ToString" and get the name instead of the integer value. Is there a way I can do...
2
by: MikalE | last post by:
Hi, I'll try and explain my problem. I'm simulating an incoming event that in my application I get from a COM object. My problem is that I can't seem to figure out how to convert an object back...
10
by: Doug | last post by:
I have an enum that will look kind of like this: private enum MyEnum { SomeValue = 0, AnotherValue = 1, ThirdValue = 2 }
4
by: =?Utf-8?B?cmF5ZnVzaW9u?= | last post by:
I am storing Keys.Right in a string and I need to convert it to back to the Keys Enumeration so I can handle it correctly. How do I do this?
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.