You need to sign in to do that
Don't have an account?
cassy
Switch- case block
Hallo everyone,
I want to write a switch-case block the convention in Java is:
switch( variable)
{
case value:
statement;
break;
case value:
statement;
break;
default
statement;
break;
}
or is it different in Apex because I get an exception.
Save error: unexpected token: '{' after switch(variable).
Apex doesn't have this concept I'm afraid - you are back to using if/else statements.
All Answers
Apex doesn't have this concept I'm afraid - you are back to using if/else statements.
Thanks bob_buzzard :)
IMHO, don't use long if/else if/else sequences if you can avoid it; use a command pattern instead. Set up a map of the "values" that can be seen to the commands to perform the processing for those different values, something like the following in Apex:
Why go to this trouble?
Salesforce is supporting now from summer 18 release onwards.
switch on expression {
when value1 { // when block 1 // code block 1 }
when value2 { // when block 2 // code block 2 }
when value3 { // when block 3 // code block 3 }
when else { // default block, optional // code block 4 }
}