473,395 Members | 1,456 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,395 software developers and data experts.

Converting query parameter to enum

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?
Jul 28 '08 #1
10 6080
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]
- mv*@spam.guard.caspershouse.com

"Doug" <dn******@dtgnet.comwrote in message
news:12**********************************@56g2000h sm.googlegroups.com...
>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?

Jul 28 '08 #2
On Jul 28, 1:52*pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
* * * * * - m...@spam.guard.caspershouse.com

"Doug" <dnlwh...@dtgnet.comwrote in message

news:12**********************************@56g2000h sm.googlegroups.com...
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?- 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?
Jul 28 '08 #3
Doug,

This is the documentation for the Parse method:

http://msdn.microsoft.com/en-us/libr...num.parse.aspx

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Doug" <dn******@dtgnet.comwrote in message
news:1c**********************************@p25g2000 hsf.googlegroups.com...
On Jul 28, 1:52 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

"Doug" <dnlwh...@dtgnet.comwrote in message

news:12**********************************@56g2000h sm.googlegroups.com...
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?- 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?
Jul 28 '08 #4
On Jul 28, 3:06*pm, Doug <dnlwh...@dtgnet.comwrote:
On Jul 28, 1:52*pm, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
Doug,
* * You can call the static Parse method on the Enum class, passingthe 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]
* * * * * - m...@spam.guard.caspershouse.com
"Doug" <dnlwh...@dtgnet.comwrote in message
news:12**********************************@56g2000h sm.googlegroups.com...
>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?- 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
Jul 28 '08 #5
On Jul 28, 2:24*pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Doug,

* * This is the documentation for the Parse method:

http://msdn.microsoft.com/en-us/libr...num.parse.aspx

--
* * * * * - Nicholas Paldino [.NET/C# MVP]
* * * * * - m...@spam.guard.caspershouse.com

"Doug" <dnlwh...@dtgnet.comwrote in message

news:1c**********************************@p25g2000 hsf.googlegroups.com...
On Jul 28, 1:52 pm, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com
"Doug" <dnlwh...@dtgnet.comwrote in message
news:12**********************************@56g2000h sm.googlegroups.com...
>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?- 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 -
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.
Jul 28 '08 #6
On Jul 28, 3:31*pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Jul 28, 3:06*pm, Doug <dnlwh...@dtgnet.comwrote:


On Jul 28, 1:52*pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
* * * * * - m...@spam.guard.caspershouse.com
"Doug" <dnlwh...@dtgnet.comwrote in message
>news:12**********************************@56g2000 hsm.googlegroups.com....
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...
Jul 28 '08 #7
Doug <dn******@dtgnet.comwrote:
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);
}
}

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 29 '08 #8
On Jul 29, 12:20*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Doug <dnlwh...@dtgnet.comwrote:
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?

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 - <sk...@pobox.com>
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."

Jul 29 '08 #9
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
Jul 29 '08 #10
Doug <dn******@dtgnet.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 29 '08 #11

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

Similar topics

2
by: Thomas Bean | last post by:
Hello, I'm fairly new to DB2. I am having some difficulty converting some stored procedres from Informix that use default input parameters. Specifically, the Informix procedures I need to convert...
2
by: msnews.microsoft.com | last post by:
How do I get Intellisense to open a dropdown list box for a method's parameters when the parameter is an ENUM? public class MyClass { public enum IDkind { PersonID, EntityID, PlaceID
16
by: Simon | last post by:
Hi all, I think I've seen someone passing an emumeration in code before. Can anyone tell me if thats possible and why i would want to. Many thanks Kindest Regards
6
by: | last post by:
I'm new to VS.NET, C#, and the enumerated datatype. I'm told that VS.NET 2005 Intellisense will pop up the members of an enum as a selection list if you are using the enum as a method parameter....
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...
3
by: ajmastrean | last post by:
I have a bunch of enumerators. I need to pass them into a function that is designed to write all values of any enumerator to the console. For instance... public enum Days : int { Sunday = 1,...
17
by: > Adrian | last post by:
I have converted a number of applications to enable them to work together on a network. I have been led to believe that I can do this as follows: FileStream fs = new FileStream(some code); while...
2
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to...
1
by: Michelle Sollicito eBarster | last post by:
Ok I know a lot of asp but not a lot of asp.net and C# so please bear with me. I need some help converting.. In my asp project I have some styles in my head tag that change according to a...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.