Switch Statement not Working

The Switch statement is not evaluating numerical data (integer or float) properly, but does evaluate string data correctly. When evaluating numerical data, it falls through all the cases until it hits the ‘default’ and executes the default code.

What is disturbing is that this has been noted on the Servoy forum since 2006…egads! Search criteria: switch statement not working.

Try the following code (one example is numeric, the other text - one works, the other does not):

var answer = 1 ;

answer = plugins.dialogs.showInputDialog( “Get Answer” , “N”, answer );

switch( answer )
{
case 1.0 : ;
plugins.dialogs.showInfoDialog( ‘Test Switch N - 1’, answer, answer );
break;

case 2.0 : ;
plugins.dialogs.showInfoDialog( ‘Test Switch N - 2’, answer, answer );
break;

case 4.0 : ;
plugins.dialogs.showInfoDialog( ‘Test Switch N - 4’, answer, answer );
break;

default:
plugins.dialogs.showInfoDialog( ‘Test Switch N - Default’, answer, answer );
}

// -------------------------------------------------------------

var answer = “What” ;

answer = plugins.dialogs.showInputDialog( “Get Answer” , “A”, answer );

switch( answer )
{
case “Save”: ;
{
plugins.dialogs.showInfoDialog( ‘Test Switch A - Save’, answer, answer );
// break;
}
case “WhatEver”: ;
{
plugins.dialogs.showInfoDialog( ‘Test Switch A - WhatEver’, answer, answer );
break;
}
case “What”: ;
{
plugins.dialogs.showInfoDialog( ‘Test Switch A - What’, answer, answer );
break;
}
default:
plugins.dialogs.showInfoDialog( ‘Test Switch A - Default’, answer, answer );
}

We know that we can work around this using ‘if, else if’ clauses, but wanted to bring this to your attention. This is a rather severe bug, and we can’t tell if this is a Servoy issue or JVM issue.

We are using Servoy Developer 3.5.5 on Windows XP and Vista.

That example should not really work, at least not reliably. showInputDialog returns a string, not an integer or float, so you should explicitly coerce the values if you want to do any type of comparison.

Do a parseFloat(answer) on the return value before the switch.

Try inserting “application.output( typeof(answer) )” after the input dialog…you’ll get “string”.

greg.

Greg,

Ah, yes, you quite correct regarding the return type of string! However, we originally built the case statement with numerics fed from a radio button with the attached valuelist:

Protocol Number | 0
Principal Investigator | 1
Title | 2

and, it still failed (uisng integer numberics in each case statement). Does the radio button valuelist also return a string value as well?

Thanks!

The value returned from the radio button should depend on the datatype of the data provider bound to the field on the form, I believe. So, if you have that value list bound to a TEXT field, it will be a string – if it’s bound to an INTEGER field, it should be an int.

Still, I’d like to see the failing code – because, actually, your example doesn’t fall through to the default case OMM.

That said, implicit coercion isn’t a good plan. If your bound data field (global or column), your cases should be of the same type.

You could also try an explicit truth test, like:

switch(true)
{
    case answer == 1 : ...
    case answer == 2 : ...
    etc.
}

And see what happens.

greg.