Reply
If Then Else - not working
Old 11-16-2007, 09:22 AM If Then Else - not working
pjb007's Avatar
Super Talker

Posts: 100
Location: UK
Can anyone solve this problem, I have a If Then Else statement which does not seem to work.

Is there a problem with this code

Code:
<% 
If request("lang") = "1" Then 
Response.Write "Language 1" 
ElseIf request("lang") = "2" 
Response.Write "Language 2" 
Else 
response.redirect "index.asp?lang=1"
End If 
%>
__________________
pjb007
pjb007 is offline
Reply With Quote
View Public Profile Visit pjb007's homepage!
 
When You Register, These Ads Go Away!
Old 11-16-2007, 09:26 AM Re: If Then Else - not working
pjb007's Avatar
Super Talker

Posts: 100
Location: UK
As soon as I posted I saw the problem! Its now working.
__________________
pjb007
pjb007 is offline
Reply With Quote
View Public Profile Visit pjb007's homepage!
 
Old 11-16-2007, 10:58 AM Re: If Then Else - not working
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,945
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
For those who didn't see the problem (I did in that it's a missing "Then" on the third line), can you post your solution, please and thanks?

Also, in order to improve the efficiency of your code, you should write your request to a variable in the form Request.X ("lang") (where X is the type of request that it is...also speeds up your code). You'll get something like:
Code:
Dim Language
Language = Request.Form ("lang") ' Assuming it's a form
if Language = "1" then
...
Every time you use Request, you have to make a call to the server for it. It's also easier to read if it's a variable.
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 11-16-2007, 02:16 PM Re: If Then Else - not working
chrishirst's Avatar
Super Moderator

Posts: 13,610
Location: Blackpool. UK
Or you could use a select ... case

Code:
with response
select case request.querystring("lang")
      case 1 
          .write "Language 1"
      case 2 
          .write "Language 2"
      case else
           .redirect index.asp?lang=1
end select
end with
Note assuming the use of querystring given the redirect URL
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-16-2007, 02:41 PM Re: If Then Else - not working
Learning Newbie's Avatar
Moderator

Posts: 5,199
Name: John Alexander
Why do people use select case statements? That always seemed like extra scaffolding code to me?

Same question for with blocks, I guess?
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 11-17-2007, 04:19 AM Re: If Then Else - not working
chrishirst's Avatar
Super Moderator

Posts: 13,610
Location: Blackpool. UK
For me;

code readability on both counts
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-17-2007, 10:30 AM Re: If Then Else - not working
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,945
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
I've never been a fan of case statements either, particularly when I have multiple variables, items, etc. to compare. But to each their own, as long as it works.
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 11-17-2007, 03:18 PM Re: If Then Else - not working
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,024
Name: Forrest Croce
Location: Seattle, WA
I generally prefer if/then/else blocks over select case, but when you need to use one branching construct inside another, it's a lot more readable to use code like:

Code:
Select Case value
   Case External.StyleSheet:
      If UserAgent = MSIE_6 Then
         RenderStyleSheet "ie6.css"
      Else If UserAgent = MSIE_7 Then
         RenderStyleSheet "ie7.css"
      Else
         RenderStyleSheet "main.css"
      End If
   Case External.javascript:
      ...
You could do that with nested if statements, but it's easier to read the inner ones when they're wrapped in a different type of flow control.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Reply     « Reply to If Then Else - not working
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15774 seconds with 12 queries