Converting query parameter to enum

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

I have an enum that will look kind of like this:

private enum MyEnum
{
SomeValue = 0,
AnotherValue = 1,
ThirdValue = 2
}


I have an url that will pass in a value like this that I want to put
into that enum:

http://ABCSite/MyPage.aspx?MyParameter=SomeValue

I'd like to use the Request.QueryString to retrieve the MyParameter
value (which equals "SomeValue") and convert that to an enum of type
MyEnum. Is there a quick and easy way to do it, or am I going to have
to build a select case to check all potential values that could come
in?
 
Doug,

You can call the static Parse method on the Enum class, passing the type
of the Enum as well as the string representing the field, and then it should
return the value to you.
 
Doug,

    You can call the static Parse method on the Enum class, passing the type
of the Enum as well as the string representing the field, and then it should
return the value to you.

--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)




I have an enum that will look kind of like this:
   private enum MyEnum
   {
       SomeValue = 0,
       AnotherValue = 1,
       ThirdValue = 2
   }
I have an url that will pass in a value like this that I want to put
into that enum:

I'd like to use the Request.QueryString to retrieve the MyParameter
value (which equals "SomeValue") and convert that to an enum of type
MyEnum.  Is there a quick and easy way to do it, or am I going to have
to build a select case to check all potential values that could come
in?- Hide quoted text -

- Show quoted text -

I am not seeing a Parse function within either just the enum object,
my actual enum object or a variable I create with a type of my enum
object. Can you send me a link to review the Parse function for
enum's?
 
Doug,

This is the documentation for the Parse method:

http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Doug,

You can call the static Parse method on the Enum class, passing the type
of the Enum as well as the string representing the field, and then it
should
return the value to you.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I have an enum that will look kind of like this:
private enum MyEnum
{
SomeValue = 0,
AnotherValue = 1,
ThirdValue = 2
}
I have an url that will pass in a value like this that I want to put
into that enum:

I'd like to use the Request.QueryString to retrieve the MyParameter
value (which equals "SomeValue") and convert that to an enum of type
MyEnum. Is there a quick and easy way to do it, or am I going to have
to build a select case to check all potential values that could come
in?- Hide quoted text -

- Show quoted text -

I am not seeing a Parse function within either just the enum object,
my actual enum object or a variable I create with a type of my enum
object. Can you send me a link to review the Parse function for
enum's?
 
I am not seeing a Parse function within either just the enum object,
my actual enum object or a variable I create with a type of my enum
object.  Can you send me a link to review the Parse function for
enum's?- Hide quoted text -

- Show quoted text -

Weird, try:
System.Enum.Parse
 
Doug,

    This is the documentation for the Parse method:

http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx

--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)


You can call the static Parse method on the Enum class, passing the type
of the Enum as well as the string representing the field, and then it
should
return the value to you.
- Show quoted text -

I am not seeing a Parse function within either just the enum object,
my actual enum object or a variable I create with a type of my enum
object.  Can you send me a link to review the Parse function for
enum's?- Hide quoted text -

- Show quoted text -

Are you sure that's not for parsing out multiple values? I put this
into my code:

MyEnum aEnum = (MyEnum)Enum.Parse(typeof(MyEnum), "SomeValue");

and got an error "Requested value "SomeValue" was not found.
 
Doug,
    You can call the static Parse method on the Enum class, passing the type
of the Enum as well as the string representing the field, and then itshould
return the value to you.
--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)

I have an enum that will look kind of like this:
   private enum MyEnum
   {
       SomeValue = 0,
       AnotherValue = 1,
       ThirdValue = 2
   }
I have an url that will pass in a value like this that I want to put
into that enum:
http://ABCSite/MyPage.aspx?MyParameter=SomeValue
I'd like to use the Request.QueryString to retrieve the MyParameter
value (which equals "SomeValue") and convert that to an enum of type
MyEnum.  Is there a quick and easy way to do it, or am I going tohave
to build a select case to check all potential values that could come
in?- Hide quoted text -
- Show quoted text -
I am not seeing a Parse function within either just the enum object,
my actual enum object or a variable I create with a type of my enum
object.  Can you send me a link to review the Parse function for
enum's?- Hide quoted text -
- Show quoted text -

Weird, try:
System.Enum.Parse- Hide quoted text -

- Show quoted text -

Same thing...
 
Doug said:
Are you sure that's not for parsing out multiple values? I put this
into my code:

MyEnum aEnum = (MyEnum)Enum.Parse(typeof(MyEnum), "SomeValue");

and got an error "Requested value "SomeValue" was not found.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's one which shows it working:

using System;

enum Foo
{
Bar,
Baz,
Qux
}

class Test
{
static void Main()
{
Foo x = (Foo) Enum.Parse(typeof(Foo), "Baz");
Console.WriteLine(x);
}
}
 
Could you post a short but complete program which demonstrates the
problem?

Seehttp://www.pobox.com/~skeet/csharp/complete.htmlfor details of
what I mean by that.

Here's one which shows it working:

using System;

enum Foo
{
    Bar,
    Baz,
    Qux

}

class Test
{
    static void Main()
    {
        Foo x = (Foo) Enum.Parse(typeof(Foo), "Baz");
        Console.WriteLine(x);
    }

}

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com


public partial class MyClass : System.Web.UI.Page
{
private enum MyEnum
{
MyFirstChoice = 0,
MySecondChoice = 1,
MyThirdChoice = 2
}

protected void Page_Load(object sender, EventArgs e)
{

MyEnum theEnum = (MyEnum)Enum.Parse(typeof(MyEnum),
Request.QueryString["MyValue"]);

}

}

I have a url that contains a value (in this example) called "MyValue"
and is set to "MySecondChoice". When I run this code I get the error
"Request value 'MySecondChoice' was not found."
 
Well, I've just tried it with the query-string ?MyValue=MySecondChoice

and it worked just fine. Double check for typing errors; also perhaps
i18n might be an issue for some dodgy characters. Finally, ensure that
there aren't any scoping/resolution issues - i.e. the MyEnum you are
talking to is the one you think it is (which can be a problem if you've
named the real code something similar to a BCL enum).

Marc
 
Doug said:
public partial class MyClass : System.Web.UI.Page
{
private enum MyEnum
{
MyFirstChoice = 0,
MySecondChoice = 1,
MyThirdChoice = 2
}

protected void Page_Load(object sender, EventArgs e)
{

MyEnum theEnum = (MyEnum)Enum.Parse(typeof(MyEnum),
Request.QueryString["MyValue"]);

}

}

I have a url that contains a value (in this example) called "MyValue"
and is set to "MySecondChoice". When I run this code I get the error
"Request value 'MySecondChoice' was not found."

Let's convert that into a short but complete program, as requested:

using System;

public partial class MyClass
{
private enum MyEnum
{
MyFirstChoice = 0,
MySecondChoice = 1,
MyThirdChoice = 2
}

static void Main()
{
MyEnum theEnum = (MyEnum)Enum.Parse(typeof(MyEnum),
"MySecondChoice");

Console.WriteLine(theEnum);
}
}

That runs without a problem. Now, when I try an invalid value I get
"Requested value..." instead of "Request value..." as an example, which
suggests you haven't cut and pasted the actual exception directly. I
suspect that if you do so, you'll find that it wasn't "MySecondChoice"
that failed but some slight variation on it.
 
Back
Top