PC Review


Reply
Thread Tools Rate Thread

Converting String to User Defined Type

 
 
=?Utf-8?B?Z21jY2FsbHVt?=
Guest
Posts: n/a
 
      8th Apr 2005
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?U2ltb25U?=
Guest
Posts: n/a
 
      8th Apr 2005
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
>

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Z21jY2FsbHVt?=
Guest
Posts: n/a
 
      8th Apr 2005
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
> >

 
Reply With Quote
 
Chris Priede
Guest
Posts: n/a
 
      8th Apr 2005
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 ((E-Mail Removed))


 
Reply With Quote
 
=?Utf-8?B?Um9iZXJ0IFcu?=
Guest
Posts: n/a
 
      21st Oct 2005
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
> >

 
Reply With Quote
 
Derrick
Guest
Posts: n/a
 
      25th Oct 2005
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." <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning User Passwords - User-Defined Type Not Defined error =?Utf-8?B?TWFjTnV0MjAwNA==?= Microsoft Access Security 1 11th Mar 2006 05:12 PM
User-defined data type; Error: Only User-defined types... =?Utf-8?B?dGlnZXJfUFJN?= Microsoft Excel Programming 1 18th Jul 2004 03:32 PM
User Defined type not defined Karpagam Microsoft Excel Programming 1 16th May 2004 05:41 PM
Why "User-defined type not defined" Shatin Microsoft Excel Programming 4 2nd Feb 2004 03:45 PM
Word.Document - user defined type not defined jowatkins Microsoft Excel Programming 1 20th Jan 2004 01:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:13 PM.