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

Converting String to User Defined Type

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 = "MyEnum.One";

// This doesn't work (obviously), but effectively this
// is what I am trying to accomplish.
MyEnum ConvertedString = (MyEnum)MyString;

Is there a way using the TYPE object or reflection to accomplish this goal?
I am a pretty advanced developer and understand most topics and commands,
so feel free to offer any ideas.

I know the example is silly and why would anyone do this, but
I am really trying to read these enum values from a text file and
assign them to a variable within a class I have, but it would have
clouded the question if I showed all of that code.
I hope the simple example is enough?

Thanks for any help.

Greg

Nov 17 '05 #1
5 5828
string MyString = "One";

MyEnum ConvertedString = (MyEnum)Enum.Parse(typeof(MyEnum), MyString, true);
"gmccallum" wrote:
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 = "MyEnum.One";

// This doesn't work (obviously), but effectively this
// is what I am trying to accomplish.
MyEnum ConvertedString = (MyEnum)MyString;

Is there a way using the TYPE object or reflection to accomplish this goal?
I am a pretty advanced developer and understand most topics and commands,
so feel free to offer any ideas.

I know the example is silly and why would anyone do this, but
I am really trying to read these enum values from a text file and
assign them to a variable within a class I have, but it would have
clouded the question if I showed all of that code.
I hope the simple example is enough?

Thanks for any help.

Greg

Nov 17 '05 #2
Thank you so much. I don't know why I didn't even look for that when I knew
that Int32.Parse existed.
"SimonT" wrote:
string MyString = "One";

MyEnum ConvertedString = (MyEnum)Enum.Parse(typeof(MyEnum), MyString, true);
"gmccallum" wrote:
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 = "MyEnum.One";

// This doesn't work (obviously), but effectively this
// is what I am trying to accomplish.
MyEnum ConvertedString = (MyEnum)MyString;

Is there a way using the TYPE object or reflection to accomplish this goal?
I am a pretty advanced developer and understand most topics and commands,
so feel free to offer any ideas.

I know the example is silly and why would anyone do this, but
I am really trying to read these enum values from a text file and
assign them to a variable within a class I have, but it would have
clouded the question if I showed all of that code.
I hope the simple example is enough?

Thanks for any help.

Greg

Nov 17 '05 #3
gmccallum wrote:
I am trying to convert the value of a string to a defined enum
value such as follows. Is there a way using the TYPE object
or reflection to accomplish this goal?


Unless I am completely misunderstanding your question -- I think you are
looking for Enum.Parse().

http://msdn.microsoft.com/library/en...parsetopic.asp
--
Chris Priede (pr****@panix.com)
Nov 17 '05 #4
Simon (et al),

I like your approach:
MyEnum ConvertedString = (MyEnum)Enum.Parse(typeof(MyEnum), MyString, true);

But is there a way to make such a conversion if I can't explicitly
(hard-code) the Enum type into the statement?

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

"SimonT" wrote:
string MyString = "One";

MyEnum ConvertedString = (MyEnum)Enum.Parse(typeof(MyEnum), MyString, true);
"gmccallum" wrote:
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 = "MyEnum.One";

// This doesn't work (obviously), but effectively this
// is what I am trying to accomplish.
MyEnum ConvertedString = (MyEnum)MyString;

Is there a way using the TYPE object or reflection to accomplish this goal?
I am a pretty advanced developer and understand most topics and commands,
so feel free to offer any ideas.

I know the example is silly and why would anyone do this, but
I am really trying to read these enum values from a text file and
assign them to a variable within a class I have, but it would have
clouded the question if I showed all of that code.
I hope the simple example is enough?

Thanks for any help.

Greg

Nov 17 '05 #5
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:81**********************************@microsof t.com...
Simon (et al),

I like your approach:
MyEnum ConvertedString = (MyEnum)Enum.Parse(typeof(MyEnum), MyString,
true);

But is there a way to make such a conversion if I can't explicitly
(hard-code) the Enum type into the statement?

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

"SimonT" wrote:
string MyString = "One";

MyEnum ConvertedString = (MyEnum)Enum.Parse(typeof(MyEnum), MyString,
true);
"gmccallum" wrote:
> 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 = "MyEnum.One";
>
> // This doesn't work (obviously), but effectively this
> // is what I am trying to accomplish.
> MyEnum ConvertedString = (MyEnum)MyString;
>
> Is there a way using the TYPE object or reflection to accomplish this
> goal?
> I am a pretty advanced developer and understand most topics and
> commands,
> so feel free to offer any ideas.
>
> I know the example is silly and why would anyone do this, but
> I am really trying to read these enum values from a text file and
> assign them to a variable within a class I have, but it would have
> clouded the question if I showed all of that code.
> I hope the simple example is enough?
>
> Thanks for any help.
>
> Greg
>

Nov 17 '05 #6

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

Similar topics

2
by: Robert W. | last post by:
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)...
7
by: Desmond Cassidy | last post by:
Hi, I have being trying to get a grip of HTML data manipulation and am using the mshtml class and System.Net to retriver HTML pages. Now as far as I can see, one can read HTML in different ways....
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
4
by: pmcgover | last post by:
I enjoyed Paul Barry's September article in Linux Journal entitled, "Web Reporting with MySQL, CSS and Perl". It provides a simple, elegant way to use HTML to display database content without any...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
15
by: allthecoolkidshaveone | last post by:
I want to convert a string representation of a number ("1234") to an int, with overflow and underflow checking. Essentially, I'm looking for a strtol() that converts int instead of long. The...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
1
by: dishal | last post by:
Can anyone help me please? How do I convert these codes to launch from a JFrame instead of a Java Applet? A simple program where the user can sketch curves and shapes in a variety of...
3
by: Dhananjay | last post by:
Hi All, I am facing problem when i am converting C#.net code(Delegate concept) into vb.net. I am unable to do that . Can someone help me to solve the problem. I am providing my C#.net code. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.