Advertisement
Guest User

Case statement with string (the array method)

a guest
Sep 29th, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.03 KB | None | 0 0
  1. type
  2.   TCatCaseLabel = record
  3.     name: string;
  4.     id: integer;
  5.   end;
  6.  
  7. function CatCaseOf(const s: string; labels: array of TCatCaseLabel;
  8.   const casesensitive: Boolean = true): integer;
  9. var
  10.   i: integer;
  11. begin
  12.   result := -1; // label not found
  13.   for i := 0 to high(labels) do
  14.   begin
  15.     if casesensitive then
  16.     begin
  17.       if s = labels[i].name then
  18.         result := labels[i].id;
  19.     end
  20.     else
  21.     begin
  22.       if lowercase(s) = lowercase(labels[i].name) then
  23.         result := labels[i].id;
  24.     end;
  25.     if result <> -1 then
  26.       break;
  27.   end;
  28. end;
  29.  
  30. procedure TForm1.Button1Click(Sender: TObject);
  31. const
  32.  ana = 1;
  33.  roberto = 2;
  34.  lucia = 3;
  35. const
  36.  labels : array [1..3] of TCatCaseLabel =
  37.  (
  38.  (name:'ana';id:ana),
  39.  (name:'roberto';id:roberto),
  40.  (name:'lucia';id:lucia)
  41.  );
  42. begin
  43.   case CatCaseOf(edit1.text,labels) of
  44.     ana: form1.caption:='ana!';
  45.     roberto: form1.caption:='roberto!';
  46.     lucia: form1.caption:='lucia!';
  47.   else
  48.     form1.Caption:=edit1.text+' not in case list!';
  49.   end;
  50. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement