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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple variable problem 1

Status
Not open for further replies.

Jimuniguy

Technical User
Mar 6, 2002
363
GB
Hi,

My coding for variables falls down when the the request.querystring empty or the varibale is empty. Please could you point out how it should be.

Code:
<%
Dim Variable10, VariableA

If Request.QueryString(&quot;switch&quot;) = 1 then  
Variable10 = off
Else
If Request.QueryString(&quot;switch&quot;) = 2 then 
Variable10 = on
Else
If Request.QueryString(&quot;switch&quot;) = none existant 
then
variable10 must be empty
End

If variable10 = empty vaule
then end
Else
If variable10 = off
Then variableA = off
Else
VariableA = on

Thanks

James
 
Variables from the Request collection always return something. If they don't have a value or don't exist than they retun an empty string, ie &quot;&quot;

The other thing that seems to be a problem above is that your Else If statements should be ElseIf in one line, like so:
Code:
If Request.QueryString(&quot;switch&quot;) = &quot;1&quot; then  
   Variable10 = &quot;off&quot;
ElseIf Request.QueryString(&quot;switch&quot;) = &quot;2&quot; then 
   Variable10 = &quot;on&quot;
ElseIf Request.QueryString(&quot;switch&quot;) = &quot;&quot; Then
   Variable10 = &quot;&quot;
End If

Then later when you need to check the value of Variable10 you can simpkly check it against an empty string &quot;&quot;.

-Tarwn


[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
minilogo.gif alt=tiernok.com
The never-completed website
 
Hi

Right, so is this correct then:

Code:
If Request.QueryString(&quot;switch&quot;) = &quot;1&quot; then  
   Variable10 = &quot;off&quot;
ElseIf Request.QueryString(&quot;switch&quot;) = &quot;2&quot; then 
   Variable10 = &quot;on&quot;
ElseIf Request.QueryString(&quot;switch&quot;) = &quot;&quot; Then
   Variable10 = &quot;&quot;
End If

If variable10 = &quot;&quot; then
End If
ElseIf variable10 = off Then
variableA = off
End If
Else
VariableA = on
End If

Thanks

Jim
 
Not quite. Think of this as blocks. An If Then block begins with an If Then statement:
Code:
If [i]statement[/i] Then
   [i]procedures[/i]

It can then have 0+ ElseIf statements, followed by either 0 or 1 Else statements, finally followed by an End If statement to show the script your done with this whole If block.

Pretend this is natural language for a moment. You would say something like:
&quot;If this is true I am going to do TaskA, If it isn't and this other case is true, I'm going to do TaskB, if all else fails then I guess I will do TaskC&quot;
You would not say &quot;If it isn't and this other case is true I'm going to do task B&quot;. People would look at you oddly because they don't know what &quot;it&quot; is because you didn't have any statements prior to it.

The original statement would look like:
Code:
If [i]this is true[/i] Then
   TaskA
ElseIf [i]other case is true[/i] Then
   TaskB
Else
   TaskC
End If

The problem with your statement above is that you supplying EndIf's to soon.
Code:
If variable10 = &quot;&quot; then      'If variable 10 is undefined
End If                       'End this statement
ElseIf variable10 = off Then '? First problem line, this ElseIf needs to be in an If/End If block
variableA = off
End If
Else                         'Same problem as above
VariableA = on
End If

In this case you will oly need one End If. Only If's get End If's:
Code:
If variable10 = &quot;&quot; then
   'Do Nothing
ElseIf variable10 = off Then
   variableA = off
Else
   VariableA = on
End If
Now we are saying:
If variable10 is empty, don't do anything. Otherwise if variable10 is off, make variableA off. If none of the above are true, make VariableA on.

-Tarwn

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
minilogo.gif alt=tiernok.com
The never-completed website
 
Sorry, ig nore the
Code:
[i][/i]'s
all over the place, I added the code tags to everything that wasn't nailed down and they over rode my italics tags :p
 
Hi,

No worries, its very helpful when someone explains whats going on, rather then just changing the code!

James
 
Hi,

Well it works, but it doesnt seem to update itself :( It would seem the 2nd IF/Then statement does not process

James
 
My mistake, you need some quotes around those on's and off's in the second if statement since they are strings. I was typing quickly and focussing to hard on the if-then syntax. Sorry about that

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
minilogo.gif alt=tiernok.com
The never-completed website
 
Hi,

Fixed it, I was refreshing the page without giving the variables a chance to do their magic

James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top