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

Home Posts Topics Members FAQ

C# Typecast to Enum from Request.Params

109 New Member
Sorry if this is not the place for C#.

in short:

public enum MyEnum
{
One,
Two,
Three
};

page_load
{
MyEnum num = (MyEnum)Request .Params("number ");
}

assume the page is being called

www.mypage.com? number="One"

i get a simple "Error 7 Cannot convert type 'string' to 'MyEnum' .... "

Thanks in advance
Mar 24 '08 #1
8 3939
nateraaaa
663 Recognized Expert Contributor
Try converting "number" to System.Int32 then cast it as an enum.

Nathan
Mar 24 '08 #2
SpecialKay
109 New Member
Dont see how that would change anything. But i tried it anyway.
Same problem, unable to convert.
Mar 24 '08 #3
SpecialKay
109 New Member
Sorry if this is not the place for C#.

in short:

public enum MyEnum
{
One,
Two,
Three
};

page_load
{
MyEnum num = (MyEnum)Request .Params("number ");
}

assume the page is being called

www.mypage.com? number="One"

i get a simple "Error 7 Cannot convert type 'string' to 'MyEnum' .... "

Thanks in advance


My solution:
Im sure there is a better way!

Here is my solution. Im sure there is a better way.

page_load
{
myEnum num = setState(Reques t.Params("numbe r");
}

private myEnum setState(string state)
{
switch(state)
{
case One:
return myEnum.One;

etc.
}
}
Mar 25 '08 #4
Plater
7,872 Recognized Expert Expert
I forget where it is found (System.Enum or something?) That will give you an enum[] type deal that lets you itterate through your ENUMs like that, then you can compare the enum's .ToString(), which in your case is "one" "two" "three", and do a string comparison to deterimine if it's your enum.

See this post for more on enum enumeration:
http://www.thescripts.com/forum/thread733730.html
Mar 25 '08 #5
balabaster
797 Recognized Expert Contributor
Is this what you need?
Expand|Select|Wrap|Line Numbers
  1. string s = "FirstItem"
  2. //Assume you have an enumeration called "MyEnum", of which each of the entries are FirstItem = 1; SecondItem = 2 etc etc.  You could find the item called "FirstItem" using the string and System.Enum.Parse...
  3. MyEnum SelectedEnum = System.Enum.Parse(GetType(MyEnum), s)
Mar 25 '08 #6
SpecialKay
109 New Member
Looks like it might work, i will try it out and let you know, thanks a bunch!
Mar 25 '08 #7
SpecialKay
109 New Member
[quote=SpecialKa y]My solution:
Im sure there is a better way!

Here is my solution. Im sure there is a better way.

page_load
{
myEnum num = setState(Reques t.Params("numbe r");
}

Thanks guys, you pointed me down the right path
the end result is

Expand|Select|Wrap|Line Numbers
  1.  
  2. public void results(httpRequest Request)
  3. {
  4.   string state = request.params["number"];
  5.  
  6.   myEnum num = (myEnum)System.Enum.Parse(typeof(myEnum), state)
  7. }
  8.  
  9.  
Mar 25 '08 #8
balabaster
797 Recognized Expert Contributor
[quote=SpecialKa y]
My solution:
Im sure there is a better way!

Here is my solution. Im sure there is a better way.

page_load
{
myEnum num = setState(Reques t.Params("numbe r");
}

Thanks guys, you pointed me down the right path
the end result is

Expand|Select|Wrap|Line Numbers
  1.  
  2. public void results(httpRequest Request)
  3. {
  4. string state = request.params["number"];
  5.  
  6. myEnum num = (myEnum)System.Enum.Parse(typeof(myEnum), state)
  7. }
  8.  
  9.  
Oops, looks like my VB to C# translation skills are slipping... hehe. At least you got it sorted ;o)

GetType = typeof <committing to memory...>
Mar 25 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3663
by: 2003et | last post by:
How can I understand a Request.Params object's type? I need this: User enters www.domain.com/myfile.aspx?6 then myfile.aspx.cs use "SELECT name FROM people WHERE id="+Request.Params; so if the user enters the parameter as string instead of integer: www.domain.com/myfile.aspx?test
9
21496
by: Dan | last post by:
I am trying to use Request.Form("__EVENTTARGET") to get the name of the control that caused a post back. It keeps returning "". I am not really sure why, this happens for all of my controls that invoke are invoking a post back. I've never used this type of method before, but I need to get the name of the control doing the postback in the Form Load event, and cannot wait until the event of the target control that runs due to the...
0
1330
by: Quinn Wilson | last post by:
I've got a breakpoint set in my code and I'm looking at 3 watches which (I think) should be functionaly equivalent" Request.Params(callingPage) "" String Request.querystring(callingPage) "CatalogBase" String Request(callingPage) "CatalogBase" String The HttpRequest documentation description for HttpRequest.Params says "Gets a combined collection of QueryString, Form, ServerVariables, and Cookies...
6
2572
by: Praveen | last post by:
As you all know the value of input, checkbox and other "user editable" elements can be retrieved on postback via Request.Params list, if you know their ID. However, if there is a span element (for example) whose value gets updated via javascript when the user does something, that value is not made available as part of the Request.Params list. Persumably because "span" is not considered a "editable" element and it's value is not sent...
7
3356
by: Tiraman | last post by:
Hi , I have 2 Questions About Using Enum 1) i have the following Enum under my class but if i need to use it i must use it like this ConstantsFileSystem.CREATE_FILE how can i use it just by the const name by doing like this CREATE_FILE
3
2040
by: oopaevah | last post by:
Hello To prevent scross site scripting I am validating each value in the Request.Params collection against the following regular expression : ^*$ This only allows the following characters : a-Z
1
1049
by: boomessh | last post by:
Hi All I have a method which takes a params of an enum (say) CheckRoles(int condition, params Activities roles) where Activities is an enum. How will i call this method?
3
3046
by: hufaunder | last post by:
Imagine you have a charting library that can draw lines, bars, floating bars, bands, etc. Lines and bars need only one input. Floating bars and bands need two inputs. There are two approaches: 1) One enum with all 4 types (bars, band, etc). One chart class that accepts up to 2 arrays of values. If the user choses a band but there is only one input array throw an exception. If the user passes two input arrays with different lengths throw...
4
1847
by: codefragment | last post by:
Hi The methods below are identical except they take as arguments different enum types. There must be a better way of writing this? ta public string ConcatValues(params EnumType1 types) {
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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
10443
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
10165
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,...
1
7543
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
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2921
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.