Recent

Author Topic: Why doesn't the "case of" statement work with strings?  (Read 1691 times)

shyub

  • Full Member
  • ***
  • Posts: 124
Why doesn't the "case of" statement work with strings?
« on: April 06, 2021, 12:45:25 pm »
"case of" is very handy for parsing, but gives an error in LAMW.

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  2. var
  3.   S: String;
  4. begin
  5.   if jRadioButton1.Checked then S:='One';
  6.   if jRadioButton2.Checked then S:='Two';
  7.   if jRadioButton3.Checked then S:='Three';
  8.   if jRadioButton4.Checked then S:='Four';
  9.   case S of LAMW
  10.     'One': jEditText1.Text:='1';
  11.     'Two': jEditText1.Text:='2';
  12.     'Three': jEditText1.Text:='3';
  13.     'Four': jEditText1.Text:='4';
  14.   end;
  15. end;

Can you fix this?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6683
Re: Why doesn't the "case of" statement work with strings?
« Reply #1 on: April 06, 2021, 01:14:53 pm »
It's your code, /you/ fix it:

Code: Pascal  [Select][+][-]
  1.   case S of
  2.     'One': jEditText1.Text:='1';
  3.     'Two': jEditText1.Text:='2';
  4.     'Three': jEditText1.Text:='3';
  5.     'Four': jEditText1.Text:='4';
  6.   end;
  7.  

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

shyub

  • Full Member
  • ***
  • Posts: 124
Re: Why doesn't the "case of" statement work with strings?
« Reply #2 on: April 06, 2021, 01:25:02 pm »
No, it does not work.
"LAMW" I accidentally added after checking.

shyub

  • Full Member
  • ***
  • Posts: 124
Re: Why doesn't the "case of" statement work with strings?
« Reply #3 on: April 06, 2021, 01:32:25 pm »
Here is the source code

rsu333

  • Full Member
  • ***
  • Posts: 110
Re: Why doesn't the "case of" statement work with strings?
« Reply #4 on: April 06, 2021, 01:38:35 pm »
I think you can avoid case ;
suppose jbutton1click
  jradiobutton[n].visible:=true;
 -------------------------------------------------------
jbutton[n]click
jedittext1.text:=[N]

MarkMLl

  • Hero Member
  • *****
  • Posts: 6683
Re: Why doesn't the "case of" statement work with strings?
« Reply #5 on: April 06, 2021, 01:45:11 pm »
Although having got rid of the obvious problem it's still an interesting question.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

shyub

  • Full Member
  • ***
  • Posts: 124
Re: Why doesn't the "case of" statement work with strings?
« Reply #6 on: April 06, 2021, 01:58:28 pm »
I ran into this problem when porting code from Windows. The code converts the dxf-file to coordinates for subsequent rendering.

Code: Pascal  [Select][+][-]
  1. function DxfToLin(SF: String): Boolean;
  2. var
  3.   n: Integer=0;
  4.   S: String;
  5.   flagParam: boolean=true;
  6.   Xmin, Ymin: Single;
  7. begin
  8.   Result:=true;
  9.   Setlength(Mlin, 0);
  10.   SL:=TStringList.Create;
  11.   SL.LoadFromFile(SF);
  12.   DXF.Deg:=true;
  13.   DXF.k:=1;
  14.   while n<SL.Count do begin
  15.     S:=DelProbel(n);
  16.     if S='Конец' then begin
  17.       SL.Free;
  18.       Result:=true;
  19.       exit;
  20.     end;
  21.     if flagParam then begin
  22.       case S of
  23.         '$ANGDIR': begin
  24.           n:=n+1;
  25.           S:=DelProbel(n);
  26.           if S='1' then DXF.Deg:=false;
  27.         end;
  28.         '$INSUNITS': begin
  29.           n:=n+1;
  30.           S:=DelProbel(n);
  31.           case S of
  32.             '1': DXF.k:=25.4;  
  33.             '2': DXF.k:=304.8;
  34.             '4': DXF.k:=1;    
  35.             '5': DXF.k:=10;  
  36.             else begin
  37.               SL.Free;
  38.               Result:=true;
  39.               exit;
  40.             end;
  41.           end;
  42.         end;
  43.         'ENTITIES': begin
  44.           flagParam:=false;
  45.         end;
  46.       end;
  47.     end else begin
  48.       case S of
  49.         'POINT': begin
  50.           if DxfPOINT(n) then begin
  51.             SL.Free;
  52.             Result:=true;
  53.             exit;
  54.           end;
  55.         end;
  56.         'LINE': begin
  57.           if DxfLINE(n) then begin
  58.             SL.Free;
  59.             Result:=true;
  60.             exit;
  61.           end;
  62.         end;
  63.         'LWPOLYLINE': begin
  64.           if DxfLWPOLYLINE(n) then begin
  65.             SL.Free;
  66.             Result:=true;
  67.             exit;
  68.           end;
  69.         end;
  70.         'CIRCLE': begin
  71.           if DxfCIRCLE(n) then begin
  72.             SL.Free;
  73.             Result:=true;
  74.             exit;
  75.           end;
  76.         end;
  77.         'ARC': begin
  78.           if DxfARC(n) then begin
  79.             SL.Free;
  80.             Result:=true;
  81.             exit;
  82.           end;
  83.         end;
  84.         'ELLIPSE': begin
  85.           if DxfELLIPSE(n) then begin
  86.             SL.Free;
  87.             Result:=true;
  88.             exit;
  89.           end;
  90.         end;
  91.         'SPLINE': begin
  92.           if DxfSPLINE(n) then begin
  93.             SL.Free;
  94.             Result:=true;
  95.             exit;
  96.           end;
  97.         end;
  98.         'OBJECTS': n:=SL.Count; .
  99.       end;
  100.     end;
  101.   end;
  102.   SL.Free;
  103.   SeareSort();
  104.   Xmin:=Mlin[0].X;
  105.   Ymin:=Mlin[0].Y;
  106.   for n:=0 to High(Mlin) do begin
  107.     if Mlin[n].X<=Xmin then Xmin:=Mlin[n].X;
  108.     if Mlin[n].Y<=Ymin then Ymin:=Mlin[n].Y;
  109.   end;
  110.   Det.S:=SF;
  111.   Det.maxX:=0; Det.maxY:=0;
  112.   Det.nVer:=Length(Mlin);
  113.   Det.nLin:=0;
  114.   for n:=0 to High(Mlin) do begin
  115.     Mlin[n].X:=Mlin[n].X-Xmin;
  116.     Mlin[n].Y:=Mlin[n].Y-Ymin;
  117.     if Det.maxX<Mlin[n].X then Det.maxX:=Mlin[n].X;
  118.     if Det.maxY<Mlin[n].Y then Det.maxY:=Mlin[n].Y;
  119.   end;
  120.   Result:=false;
  121. end;

WooBean

  • Full Member
  • ***
  • Posts: 230
Re: Why doesn't the "case of" statement work with strings?
« Reply #7 on: April 06, 2021, 02:03:52 pm »
Hi,

your code was prepared with source lines length of 1024 characters (I looked only near "case of" line 51). Use normal Lazarus editor and keep lines reasonably long (<130 characters).

Code highligting suggested thad something [invisible characters but not white spaces] were detected at line 51 and then code completion failed at lines with string literals.

 
Problem is not related to "case of" with string selector.



« Last Edit: April 06, 2021, 03:07:21 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

shyub

  • Full Member
  • ***
  • Posts: 124
Re: Why doesn't the "case of" statement work with strings?
« Reply #8 on: April 06, 2021, 03:03:38 pm »
WooBean, I tried to set the string size and 10 and 255 characters. Did not help.
There is source code above, you can check.

wp

  • Hero Member
  • *****
  • Posts: 11912
Re: Why doesn't the "case of" statement work with strings?
« Reply #9 on: April 06, 2021, 03:12:59 pm »
AFAIK case of string works only in mode objfpc.  Is the unit set up for that?

WooBean

  • Full Member
  • ***
  • Posts: 230
Re: Why doesn't the "case of" statement work with strings?
« Reply #10 on: April 06, 2021, 03:16:10 pm »
I ment that IDE editor allows quite long lines of source code (Lazarus has 1024 characters/columns) and something at not shown part of the source editor window must be wrong.

It may be related to file importing. I recall my own old problems with Delphi when using source blocks from C++ origins.

[Edited]
wp - hit the problem
{$mode objfpc}{$H+}

« Last Edit: April 06, 2021, 03:19:20 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

shyub

  • Full Member
  • ***
  • Posts: 124
Re: Why doesn't the "case of" statement work with strings?
« Reply #11 on: April 06, 2021, 03:20:30 pm »
The problem can be solved something like this:

Code: Pascal  [Select][+][-]
  1. Name=(One, Two, Three, Four);
  2.  
  3. var
  4.   AndroidModule1: TAndroidModule1;
  5.  
  6. implementation
  7.  
  8. {$R *.lfm}
  9.  
  10.  
  11. { TAndroidModule1 }
  12.  
  13. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  14. var
  15.   S: Name; // String;
  16. begin
  17.   if jRadioButton1.Checked then S:=One;
  18.   if jRadioButton2.Checked then S:=Two;
  19.   if jRadioButton3.Checked then S:=Three;
  20.   if jRadioButton4.Checked then S:=Four;
  21.   case S of
  22.     One: jEditText1.Text:='1';
  23.     Two: jEditText1.Text:='2';
  24.     Three: jEditText1.Text:='3';
  25.     Four: jEditText1.Text:='4';
  26.   end;
  27. end;

But now the new problem is how to bind String and Name ...

 

TinyPortal © 2005-2018