Converting String to User Defined Type

Discussion in 'Microsoft C# .NET' started by Guest, Apr 8, 2005.

  1. Guest

    Guest Guest

    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
     
    Guest, Apr 8, 2005
    #1
    1. Advertisements

  2. Guest

    Guest Guest

    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
    >
     
    Guest, Apr 8, 2005
    #2
    1. Advertisements

  3. Guest

    Guest Guest

    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
    > >
     
    Guest, Apr 8, 2005
    #3
  4. Guest

    Chris Priede Guest

    Chris Priede, Apr 8, 2005
    #4
  5. Guest

    Guest Guest

    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
    > >
     
    Guest, Oct 21, 2005
    #5
  6. Guest

    Derrick Guest

    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." <> wrote in message
    news:...
    > 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
    >> >
     
    Derrick, Oct 25, 2005
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Oquendo
    Replies:
    0
    Views:
    411
    Frank Oquendo
    Jul 31, 2003
  2. Jon Skeet
    Replies:
    0
    Views:
    372
    Jon Skeet
    Jul 31, 2003
  3. Hessam
    Replies:
    0
    Views:
    947
    Hessam
    Aug 8, 2003
  4. Programmer
    Replies:
    6
    Views:
    1,125
    Jon Skeet [C# MVP]
    Jan 17, 2004
  5. Dylan Parry

    Converting ArrayList to Array of user defined objects

    Dylan Parry, Sep 15, 2005, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    238
    Dylan Parry
    Sep 15, 2005
Loading...

Share This Page