Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

switch / case problem

Status
Not open for further replies.

mookie0001

Technical User
Jun 14, 2002
23
US
Hello,

I am having problems with a switch/case function. For some reason the function always returns only the default value. Yet, curiously, if I was to do an IF(Request.QueryString("page")=="home") type statement in place of the switch, the IF statement functions correctly. My code is below. Any ideas on what I am doing wrong? Thanks in advance!

-Chris


switch(Request.QueryString("page"))
{
case "home" : {contentPage = "home.htm"; break}
case "programs" : {contentPage = "programs.htm"; break}
case "membership" : {contentPage = "membership.htm"; break}
case "notices" : {contentPage = "notices.htm"; break}
case "minutes" : {contentPage = "minutes.htm"; break}
case "links" : {contentPage = "links.htm"; break}
case "calendar" : {contentPage = "calendar.htm"; break}
case "newsletter" : {contentPage = "newsletter.htm"; break}
default : {contentPage = "home.htm"; break}
};
 
Put semicolons after your breaks. I don't think you need the semicolon after the final close-curley-brace.

Cheers,
[monkey] Edward [monkey]

"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
I tried your suggested changes with no success. Any other ideas? ...this is driving me insane, I know it must be something simple...
 
hi,
There's nothing wrong with the syntax of your switch statement. The problem is the switch statement isn't finding a match. My guess is that if you test your method "Request.QueryString()", you'll find it doesn'y return the exact string you are using to name the cases in the statement. You will have to create a separate function using regular expressions to extract and return the part of the query string from "Request.QueryString()" that matches your cases.
sundemon
 
Oh, it never occurred to me that the incoming data wasn't verified.

Right before your "switch" statement, put a

Code:
alert(Request.QueryString(\'page\'));

or somesuch statement to make sure that the string you're handing your switch statement is an actual string on its list.

Every formatting pattern I've seen for switch specifies a semicolon after the break statements, so I tend toward that thinking.

Cheers,
[monkey] Edward [monkey]

"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
if this is ina an asp page (request.querystring is asp right? just do:

switch(<%=Request.QueryString(&quot;page&quot;)%>)
{
case &quot;home&quot; : {contentPage = &quot;home.htm&quot;; break}
case &quot;programs&quot; : {contentPage = &quot;programs.htm&quot;; break}
case &quot;membership&quot; : {contentPage = &quot;membership.htm&quot;; break}
case &quot;notices&quot; : {contentPage = &quot;notices.htm&quot;; break}
case &quot;minutes&quot; : {contentPage = &quot;minutes.htm&quot;; break}
case &quot;links&quot; : {contentPage = &quot;links.htm&quot;; break}
case &quot;calendar&quot; : {contentPage = &quot;calendar.htm&quot;; break}
case &quot;newsletter&quot; : {contentPage = &quot;newsletter.htm&quot;; break}
default : {contentPage = &quot;home.htm&quot;; break}
};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top