SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Evangelist Brandon Luhring's Avatar
    Join Date
    Apr 2002
    Location
    IN, USA
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    switch/case with variable

    Is this a valid "case" line for a switch/case statement?

    Code:
    case ('CustomerFName' + i):

    I have a form that dynamically repeats a set of fields depending on how many customers the user wants to enter. Thus, I end up with a CustomerFName1, CustomerFName2, CustomerFName3, etc...

    I have an error in my code somewhere that prevents the rest of the .js file from running, and I have a suspicion that this is the area having the trouble.

    Thanks!
    Brandon

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Not sure about this, but try using eval:

    Code:
    case eval('CustomerFName' + i):
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Evangelist Brandon Luhring's Avatar
    Join Date
    Apr 2002
    Location
    IN, USA
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick reply!

    I found my main problem... A missing bracket. However this code is still not quite right...

    I have this, now:

    Code:
    case 'HaveRead':
    	alert('Prog Rules');
    	fieldName = 'Program Rules';
    	break;
    case eval('CustomerType' + i):
    	alert('Cust Type');
    	fieldName = '#' + i + ' Existing or Prospect'
    	break;
    When the code runs, I get an alert box on the 'HaveRead', but never one on the 'CustomerType', it's as if it doesn't understand...

  4. #4
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Try placing the value in a variable outside of the switch...

    Code:
    var customer_type = eval('CustomerType' + i);
    ... and then use it in your case statement:

    Code:
    case customer_type:
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you think eval() is doing to the line:

    'CustomerType' + i

    When the code runs, I get an alert box on the 'HaveRead', but never one on the 'CustomerType', it's as if it doesn't understand...
    What does i equal?

  6. #6
    SitePoint Evangelist Brandon Luhring's Avatar
    Join Date
    Apr 2002
    Location
    IN, USA
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7stud is right, I tried the variable, same thing...

    He's also right in that my "i" variable wasn't what I thought it was... It was the form.element index.

    So, I'm currently working on setting this up to loop through all form.elements, it will set a flag if it finds it so as to keep processing down. Whithin that loop, if it's not a static form.element, then I loop through the number of customers available searching for the proper names.

    It's not there yet, but I'm closer.

    Thanks guys.

  7. #7
    SitePoint Evangelist Brandon Luhring's Avatar
    Join Date
    Apr 2002
    Location
    IN, USA
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK,

    I think I have the flow correct now. However, now I'm back to my initial question....

    This is a snippet of what I'm using now:

    Code:
    for (x=1; x <= thisForm.TotalNumber.value; x++){
    	alert(x + " - " + fieldName);
    	if (!doneThisField){
    		switch(fieldName) {
    			case eval('CustomerType' + x):
    				alert('done - ' + fieldName);
    				fieldName = '#' + x + ' Existing or Prospect'
    				doneThisField = true;
    				break;
    My first alert() box will say "1 - CustomerType1", and yet, the second alert box is never fired... Again, it seems like JavaScript is ignoring my case line.

    Any clues?

    Thanks
    B

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here it is again:
    What do you think eval() is doing to the line:

    eval('CustomerType' + x)

  9. #9
    SitePoint Evangelist Brandon Luhring's Avatar
    Join Date
    Apr 2002
    Location
    IN, USA
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would obviously think that as it iterates through the For statement it would equal 'CustomerType1', 'CustomerType2', etc...

    However, when x=1 and fieldName='CustomerType1', why, then, does it not "evaluate" eval('CustomerType' + x) as 'CustomerType1' and execute the commands within that case statement??

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would obviously think that as it iterates through the For statement it would equal 'CustomerType1', 'CustomerType2', etc...
    What do you think the line:

    "CustomerType" + x

    would do?

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this script and see what conclusions you come up with:
    Code:
    var CustomerFName1 = 100;
    var i = 1;
    var result1 = "CustomerFName" + i;
    var result2 = eval("CustomerFName" + i);
    alert(result1);
    alert(result2);

  12. #12
    SitePoint Evangelist Brandon Luhring's Avatar
    Join Date
    Apr 2002
    Location
    IN, USA
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm no longer at work, but I've tested a simplified version of it here at home.

    I guess, in my previous code the eval() was trying to map a variable, which would return an empty string or null... Is that correct?

    On my simplified test, it was this that worked:

    Code:
     
     var i = 1;
     var fieldName = 'CustomerType1';
     
     switch(fieldName) {
       case ("CustomerType" + i):
    	alert("No Eval() Works.");
     }
    Which is what I had to begin with... I guess, either something else is still wrong with my code at work, or I never removed the eval() after I found that missing bracket...

    Thanks, 7stud.

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess, in my previous code the eval() was trying to map a variable, which would return an empty string or null... Is that correct?
    eval() takes a string argument, and executes the javascript code therein, and then returns a value. When the code in the string is just a variable name, eval() fetches the value of the variable. So, in the example I posted, the first line:

    var result1 = "CustomerFName" + i;

    results in a string, while the eval() line:

    var result2 = eval("CustomerFName" + i);

    results in the value stored in the variable. If there isn't a variable named CustomerFName1 that's in scope, then eval() will cause an error saying it can't find a variable by that name.

    Rule 1: don't use eval()
    Rule 2: if you use eval(), you better know how it works.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •