473,795 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.QuerySt ring 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 6136
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.co m

"Doug" <dn******@dtgne t.comwrote in message
news:12******** *************** ***********@56g 2000hsm.googleg roups.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.QuerySt ring 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.guar d.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.c om

"Doug" <dnlwh...@dtgne t.comwrote in message

news:12******** *************** ***********@56g 2000hsm.googleg roups.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.QuerySt ring 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.co m

"Doug" <dn******@dtgne t.comwrote in message
news:1c******** *************** ***********@p25 g2000hsf.google groups.com...
On Jul 28, 1:52 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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.c om

"Doug" <dnlwh...@dtgne t.comwrote in message

news:12******** *************** ***********@56g 2000hsm.googleg roups.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.QuerySt ring 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...@dtgne t.comwrote:
On Jul 28, 1:52*pm, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guar d.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.c om
"Doug" <dnlwh...@dtgne t.comwrote in message
news:12******** *************** ***********@56g 2000hsm.googleg roups.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.QuerySt ring 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.Par se
Jul 28 '08 #5
On Jul 28, 2:24*pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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.c om

"Doug" <dnlwh...@dtgne t.comwrote in message

news:1c******** *************** ***********@p25 g2000hsf.google groups.com...
On Jul 28, 1:52 pm, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guar d.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.c om
"Doug" <dnlwh...@dtgne t.comwrote in message
news:12******** *************** ***********@56g 2000hsm.googleg roups.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.QuerySt ring 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.Pa rse(typeof(MyEn um), "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...@dtgne t.comwrote:


On Jul 28, 1:52*pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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.c om
"Doug" <dnlwh...@dtgne t.comwrote in message
>news:12******* *************** ************@56 g2000hsm.google groups.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.QuerySt ring 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.Par se- Hide quoted text -

- Show quoted text -
Same thing...
Jul 28 '08 #7
Doug <dn******@dtgne t.comwrote:
Are you sure that's not for parsing out multiple values? I put this
into my code:

MyEnum aEnum = (MyEnum)Enum.Pa rse(typeof(MyEn um), "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(type of(Foo), "Baz");
Console.WriteLi ne(x);
}
}

--
Jon Skeet - <sk***@pobox.co m>
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.co mwrote:
Doug <dnlwh...@dtgne t.comwrote:
Are you sure that's not for parsing out multiple values? *I put this
into my code:
MyEnum aEnum = (MyEnum)Enum.Pa rse(typeof(MyEn um), "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.htmlfo r 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(type of(Foo), "Baz");
* * * * Console.WriteLi ne(x);
* * }

}

--
Jon Skeet - <sk...@pobox.co m>
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.P age
{
private enum MyEnum
{
MyFirstChoice = 0,
MySecondChoice = 1,
MyThirdChoice = 2
}

protected void Page_Load(objec t sender, EventArgs e)
{

MyEnum theEnum = (MyEnum)Enum.Pa rse(typeof(MyEn um),
Request.QuerySt ring["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=MySeco ndChoice

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

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

Similar topics

2
4093
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 use the '*' character as a default input parameter to be used with the 'MATCHES' condition if the input value is null. For example: CREATE PROCEDURE "informix".stored_proc(input1 DECIMAL(18,8), input2 CHAR(20) DEFAULT '*')
2
5698
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
82852
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
1943
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. This sounds very handy. I would really appreciate it if someone could give me a basic C# code sample showing how this would be set up, both the enum and the method. So let's say you're building a method, "ReturnPageType(...)", and you want an...
4
1786
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 sql markup in the cgi script. The cgi script simply calls the Mysql command line with the HTML option (-H) and the SQL script file directed to that command. This provides complete separation of the markup from the sql code. The plain vanila...
3
3192
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, Monday, Tuesday }
17
1618
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 (!fs.CanRead){allow the processor to handle another thread;} Hoverer, doing some tests I find out that the solution isn't working. Is there a simple way out?
2
3521
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 manipulate the bitfields. For instance: <Flags()_ Enum ActionFlags As Byte
1
2245
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 parameter passed in in the query string - fontfamily is the variable that I store the value of the parameter in within my asp code, and then I place that into the styles see below.. <style type="text/css"> a:link,a:visited,a:hover,a:active{font...
0
10448
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10167
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9046
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.