function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cassycassy 

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).

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Apex doesn't have this concept I'm afraid - you are back to using if/else statements.

All Answers

bob_buzzardbob_buzzard

Apex doesn't have this concept I'm afraid - you are back to using if/else statements.

This was selected as the best answer
cassycassy

Thanks bob_buzzard :)

Phil WPhil W

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:

public with sharing class MyClass {
  private abstract class Command {
    // TODO specifically choose if you want a return value and which parameters are required across all the commands (each 'case')
    abstract void execute(...);
  }

  // Apex (sadly) doesn't support anonymous implementations, so this is more verbose than it would be in java
  class FirstCommand extends Command {
    override void execute(...) {
      // Do the first command's processing
    }
  }

  class SecondCommand extends Command {
    override void execute(...) {
      // Do the second command's processing
    }
  }

  // More command implementations as needed
  ...

  // TODO specifically choose what type the values can be and initialize the map accordingly
  // NB: even though this is static, the map will be re-initialized for each session that needs it, since that's how Apex works
  private static final Map<String, Command> COMMANDS = new Map<String, Command> {
    'first' => new FirstCommand(),
    'second' => new SecondCommand(),
    ...
  };

  public void runTheSwitchStatement(...) {
    ...
    // TODO this variable's type must match the command map's key type
    String variable = ...;

    Command c = COMMANDS.get(variable);

    if (c != null) {
      // TODO pass in the required parameters and process any return value, if required
      c.execute(...);
    }
  }
}


Why go to this trouble?​

  1. It avoids evaluating lots of separate conditions; you compute the "variable" once and use its value to select the command required.
  2. It helps you modularize/break down your code into smaller but separate blocks.
  3. It allows you to write the "runTheSwitchStatement" method as short and sweet, so avoiding a potentially massively long method (code analysis of complexity should indicate it is less complex).
  4. It instills an Object-Oriented mindset.
sree r 25sree r 25
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_switch.htm

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 }
}
Phil WPhil W
The fact support now exists doesn't detract from the fact this is not object oriented. See my previous comment for an approach that is.