PDA

View Full Version : VBScript Conditional Structure Headache


Modred
06-24-2006, 11:05 PM
Alright, here's the code I have in an ASP page:

set thread = xmlObj.getElementsByTagName("post")

'if no variable submitted in querystring, idnum is empty!
set idnum = request.querystring("post")

if isempty(idnum) then
idnum=thread.length
elseif idnum<1 then
idnum=1
elseif idnum>thread.length then
idnum=thread.length
end if

The xml document loaded into xmlObj loads properly, and I know there are 3 elemens in the nodelist returned into the variable thread. So thread.length = 3, and I have written it out using response.write to verify. Given that I'm pulling idnum from the query string, it's possible an incorrect value will be passed so I need to deal with it. The second elseif appears as though it should set idnum to thread.length if and only if idnum is more than 3 (for this case, since thread.length = 3). However, this second elseif always executes, even if idnum is less than thread.length, and I cannot discover why.

If I change the code to the following:

set thread = xmlObj.getElementsByTagName("post")

'if no variable submitted in querystring, idnum is empty!
set idnum = request.querystring("post")

if isempty(idnum) then
idnum=thread.length
elseif idnum<1 then
idnum=1
elseif idnum>3 then
idnum=thread.length
end if

Everything works fine now. But in the process I've traded out using thread.length for hardcoding the value, which becomes a major hassled. What I don't understand is why the second block of code does what I expect and the first does not, when thread.length = 3. Is it something with the order of operations involving the . operator? (I'm knew to VBScript, so I'm guessing based on what I know in C++)

Anyone have some idea? If all else fails I can just recode the page using Javascript, but I'd much rather get it done in VBScript since grasping the basics of that language was somewhat the goal of this.

EDIT: Fixed. Values read from querystring are read in as strings (so obvious -_-) and comparison between a string and a number always returns the string > number. So that was my problem. Throwing in an idnum=eval(idnum) solved it all [for some reason idnum=eval(request.querystring("post")) didn't work]. And that's why I like strictly typed languages.