[Solved] How to use numeric variables in case declaration? lazarus / 62-General ---------------------------------------------------------------------- loaded · 2022-02-18 07:51 Hi All, If we use the numeric value directly in the case statements, there is no problem, but if we use it over a numeric variable, an error occurs! Sample code; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);  var    i:integer=5;    ib:integer=4;  begin    case i of    0: begin        end;    end;     case i of     ib: begin  // it gives error here!!!     [Error: Constant Expression expected]         end;     end; end;end. Why am I getting an error like this? Is there a different setting that needs to be made? ---------------------------------------------------------------------- Red_prig · 2022-02-18 07:54 operator case support only constant value ---------------------------------------------------------------------- howardpc · 2022-02-18 08:19 For testing variable values you have to use a sequence of if then tests: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if v = value1 then  Do1else if v = value2 then  Do2else if ...  etc. ---------------------------------------------------------------------- 440bx · 2022-02-18 08:25 As Red_prig stated, a case statement only supports comparisons against constants with the exception of strings. However, there is a way to "synthesize" an inline case statement that works with variables by doing something like this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for LoopVar := 1 to 1 do  { or "for i := i to i do" but only if "i" doesn't change in the "case" statement }begin  { first "case" }  if i = ib then   begin     { do whatever is appropriate when i = ib }     break;  end;   { second case }  if i = { some other variable } then  begin    { do whatever is appropriate in that case }     break;  end;    { and so on until all cases are accounted for }end; { remaining function code follows the "for"/synthesized-case statement } basically, the "for" loop is just a trick to create an inline scope to break out of.  The same thing can be done using a "repeat ... until TRUE".  Of course, the same thing can be accomplished by calling a function but, this way, the "case" is inline just as a normal "case" statement is. Personally, I prefer this solution over nested "if"s which, as howardpc stated, is the more commonly used alternative. HTH. ---------------------------------------------------------------------- loaded · 2022-02-18 08:42 Red_prig and howardpc and 440bx Thank you very much for taking your valuable time to reply. Topic understood!!! We keep the if expression on defense. ---------------------------------------------------------------------- howardpc · 2022-02-18 08:49 Since in your original example you had assigned known constant values to your variables you could also do this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  i:integer=5;  ib:integer=4;  iStr: String;begin  iStr := i.ToString;   case iStr of    '0': begin       end;    '1': begin         end;    '4': DoIB;    '5': DoI;   otherwise      DoUnexpected;  end;end; ---------------------------------------------------------------------- loaded · 2022-02-18 09:28 howardpc,Sorry we got you a little tired, it's my fault. I didn't explain my problem well at first. The process of assigning values to variables was for exemplary purposes.They are normally changed dynamically. Still, a good example is the tostring expression in particular. Thank you very much