0

first post! hello all. :)

i'm in the process of converting some vbscript into jscript at work, and not being an expert when it comes to any sort of javascript (or c syntax generally) i am now stuck. specifically with the 'switch' statement.

the application is a server-side api that returns xml based on arguments sent to it in the querystring of its url.
e.g. /server_api.asp?action=getObjectByID&id=53

in vbscript i was grabbing all the arguments from the querystring and putting them into a scripting dictionary object called "qsDetail". then i was calling each like so... qsDetail("id") ...or whatever. iterating through each key/value pair was easy as you just use 'for...each'. obviously no such thing in jscript so i went with an enumerator object, iterating through each item and passing each pair to an associative array, like so:

var qsDetail = [];
var e = new Enumerator(Request.QueryString);
for (;!e.atEnd();e.moveNext()) {
    var x = e.item();
    qsDetail[x] = Request.QueryString(x);
}

so far so good. however, in the following function the 'switch' statement simply refuses to work. putting Response.write lines in to debug it, it emerges that the value "category" is indeed being passed through to the switch statement (in the example of the url above). but it just bails. if you add "default: <something>" then it runs that line instead.

function getObjectByID&#40;&#41; &#123;
    var strSQL = "SELECT id, type, name, description FROM object WHERE id =" + qsDetail&#91;"id"&#93;;
    var oRs = oConn.Execute&#40;strSQL&#41;;
	
    if &#40;oRs.EOF&#41; &#123;
        emptyXML&#40;&#41;;
    &#125; else &#123;
        outputXML = '<?xml version="1.0" ?>\n';
        // Loop for safety reasons, despite ID being unique
        while &#40;!oRs.EOF&#41; &#123;
            switch &#40;oRs&#40;"type"&#41;&#41; &#123;
                case "category"&#58; outputXML += processCategory&#40;oRs&#40;"id"&#41;&#41;;
                case "skill"&#58; outputXML += processSkill&#40;oRS&#40;"id"&#41;&#41;;
            &#125;
            oRs.MoveNext;
        &#125;
    &#125;
&#125;

but if you replace the 'switch' statement with two 'if' statements then everything runs smoothly and it correctly goes with 'category, and the rest of the code runs fine.

if &#40;oRs&#40;"type"&#41; == "category"&#41; &#123;
    outputXML += processCategory&#40;oRs&#40;"id"&#41;&#41;;
&#125;
if &#40;oRs&#40;"type"&#41; == "skill"&#41; &#123;
    outputXML += processSkill&#40;oRs&#40;"id"&#41;&#41;;
&#125;

which is almost all well and good, except for ... why?!

i simply cannot see what is wrong with that 'switch' statement. in itself there appears to be nothing wrong, so i assume it has to be something in the context of the rest of the code. there are no problems relating to the DB or variables, as everything runs fine once you make the changes referred to above.

any jscript experts out there able to shed some light on this?

thanks in advance.

1
Contributor
1
Reply
2
Views
14 Years
Discussion Span
Last Post by Ben Martin
0

solved it myself. it was failed type conversion, that for some reason only affects the 'switch' statement. adding the change below now works:

while &#40;!oRs.EOF&#41; &#123;
    var oType = oRs&#40;"type"&#41;.value;
    switch &#40;oType&#41; &#123;

however, you have to explicitly use the 'value' property or else it will still fail.

thanks anyway guys. and hopefully this will turn out to be of use to someone else as well. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.