Eden Cheung
Eden Cheung

Reputation: 333

Is there any syntax that can shorten this code?

Is there anyway i can shorten this code? its so bulky!

switch(line){
    case 1:
      llemu::editLable(line1, fmt);
    break;
    case 2:
      llemu::editLable(line2, fmt);
    break;
    case 3:
      llemu::editLable(line3, fmt);
    break;
    case 4:
      llemu::editLable(line4, fmt);
    break;
    case 5:
      llemu::editLable(line5, fmt);
    break;
    case 6:
      llemu::editLable(line6, fmt);
    break;
    case 7:
      llemu::editLable(line7, fmt);
    break;
    case 8:
      llemu::editLable(line8, fmt);
    break;
  }

Is there a way to use a variable value as a/ a part of a variable name? Thanks for your kind help.

Upvotes: 4

Views: 80

Answers (1)

Paul Evans
Paul Evans

Reputation: 27567

Simply place all of your line1, line2, ... into an array lines. Then your code becomes:

if (line >= 1 && line <= 8) {
    llemu::editLable(lines[line - 1], fmt);
}

Upvotes: 5

Related Questions