473,804 Members | 3,797 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
10 6137
Doug <dn******@dtgne t.comwrote:
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."
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.Pa rse(typeof(MyEn um),
"MySecondChoice ");

Console.WriteLi ne(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.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 #11

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

Similar topics

2
4095
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
5699
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
82856
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
1944
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
1788
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
3193
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
1619
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
3522
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
2247
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
9589
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10085
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
9163
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
7626
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
5527
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
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
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
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.