Recent

Author Topic: Why this is not valable on FPC  (Read 494 times)

BSaidus

  • Sr. Member
  • ****
  • Posts: 302
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Why this is not valable on FPC
« on: November 08, 2019, 03:09:00 pm »
I have this Code:
Code: Pascal  [Select]
  1. function fStateMnger( pForm: TForm; aCtrls: array of TControl; eSI: TDBStateInfos) : Boolean;
  2. var
  3.   I: Integer;
  4.   wCnt: TControl;
  5.   //iOp: Integer;
  6. begin
  7.   //iOp := InsUpSv;
  8.   for I := 0 to Pred(pForm.ControlCount) do
  9.   begin
  10.     wCnt := pForm.Controls[I];
  11.     if (wCnt is TEdit) and (not wCnt in aCtrls) then // <-- do not compile : (not wCnt in aCtrls)
  12.     begin
  13.       if eSI = siBrowse then
  14.         TEdit(wCnt).Enabled := False
  15.       else if  eSI in [siInsert, siUpdate] then
  16.         TEdit(wCnt).Enabled := true;
  17.     end;
  18.     if (wCnt is TComboBox) and (not wCnt in aCtrls) then
  19.     begin
  20.       if eSI = siBrowse then
  21.         TComboBox(wCnt).Enabled := False
  22.       else if  eSI in [siInsert, siUpdate] then
  23.         TComboBox(wCnt).Enabled := true;
  24.     end;
  25.   end;
  26.  
  27. end;  
  28.  
  29.  


I do not compile.
I want to know if th IN operator is not applicabe to arrays.

Thanks.

« Last Edit: November 12, 2019, 10:46:17 am by BSaidus »
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7503
Re: Why this is not valable on FPC
« Reply #1 on: November 08, 2019, 03:10:19 pm »
IN  is for sets only

JdeHaan

  • New Member
  • *
  • Posts: 25
Re: Why this is not valable on FPC
« Reply #2 on: November 08, 2019, 03:17:33 pm »
(not (wCnt in aCtrls))

howardpc

  • Hero Member
  • *****
  • Posts: 3178
Re: Why this is not valable on FPC
« Reply #3 on: November 08, 2019, 03:31:26 pm »
You have to write it so:
Code: Pascal  [Select]
  1. function fStateMnger(pForm: TForm; aCtrls: TControlArray; eSI: TDBStateInfos): Boolean;
  2. var
  3.   I: Integer;
  4.   wCnt: TControl;
  5.  
  6.   function CtrlIsInArray(const aCtrl: TControl; anArray: TControlArray): Boolean;
  7.   var
  8.     c: TControl;
  9.   begin
  10.     for c in anArray do
  11.       if c.Equals(aCtrl) then
  12.         Exit(True);
  13.     Result := False;
  14.   end;
  15.  
  16. begin
  17.   Result := False;
  18.   for I := 0 to Pred(pForm.ControlCount) do
  19.   begin
  20.     wCnt := pForm.Controls[I];
  21.     if (wCnt is TEdit) and CtrlIsInArray(wCnt, aCtrls) then
  22.     begin
  23.       if eSI = siBrowse then
  24.       begin
  25.         TEdit(wCnt).Enabled := False;
  26.         Result := True;
  27.       end
  28.       else if eSI in [siInsert, siUpdate] then
  29.         begin
  30.           TEdit(wCnt).Enabled := True;
  31.           Result := True;
  32.         end;
  33.     end;
  34.     if (wCnt is TComboBox) and CtrlIsInArray(wCnt, aCtrls) then
  35.     begin
  36.       if eSI = siBrowse then
  37.         begin
  38.           TComboBox(wCnt).Enabled := False;
  39.           Result := True;
  40.         end
  41.       else if  eSI in [siInsert, siUpdate] then
  42.         begin
  43.           TComboBox(wCnt).Enabled := True;
  44.           Result := True;
  45.         end;
  46.     end;
  47.   end;
  48. end;


BSaidus

  • Sr. Member
  • ****
  • Posts: 302
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Why this is not valable on FPC
« Reply #4 on: November 08, 2019, 03:42:56 pm »
Thank you for Help.
I've managed one.

Code: Pascal  [Select]
  1. function IsInArray(pcCmp: TComponent; aCtr: array of TComponent ): boolean;
  2. var
  3.   I : Integer;
  4. begin
  5.   Result := False;
  6.   for I := Low(aCtr) to High(aCtr) do
  7.   begin
  8.     if pcCmp = actr[I] then
  9.     begin
  10.       Result := True;
  11.       Break;
  12.     end;
  13.   end;
  14. end;
  15.  
  16. function fStateMnger( poForm: TForm; aCtrls: array of TComponent; eSI: TDBStateInfos) : Boolean;
  17. var
  18.   I: Integer;
  19.   wCnt: TComponent;
  20.   IsIn: Boolean;
  21. begin
  22.  
  23.   Result := False ;
  24.   for I := 0 to Pred(poForm.ComponentCount) do
  25.   begin
  26.     wCnt := poForm.Components[I];
  27.     IsIn := IsInArray( wCnt, aCtrls );
  28.     // edit
  29.     if (wCnt is TEdit) and ( not IsIn )
  30.       then
  31.     begin
  32.       if eSI = siBrowse then
  33.         (wCnt as TEdit).Enabled := False
  34.       else if  eSI in [siInsert, siUpdate] then
  35.         (wCnt as TEdit).Enabled := true;
  36.     end;
  37.     // Combo
  38.     if (wCnt is TComboBox) and ( not IsIn )
  39.       then
  40.     begin
  41.       if eSI = siBrowse then
  42.         (wCnt as TComboBox).Enabled := False
  43.       else if  eSI in [siInsert, siUpdate] then
  44.         (wCnt as TComboBox).Enabled := true;
  45.     end;
  46.     // Memo
  47.     if (wCnt is TMemo) and ( not IsIn )
  48.       then
  49.     begin
  50.       if eSI = siBrowse then
  51.         (wCnt as TMemo).Enabled := False
  52.       else if  eSI in [siInsert, siUpdate] then
  53.         (wCnt as TMemo).Enabled := true;
  54.     end;
  55.   end;
  56.   Result := True ;
  57.  
  58. end;
  59.  
« Last Edit: November 08, 2019, 03:47:43 pm by BSaidus »
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

440bx

  • Hero Member
  • *****
  • Posts: 1198
Re: Why this is not valable on FPC
« Reply #5 on: November 08, 2019, 03:55:12 pm »
IN  is for sets only
Not to nitpick but, in FPC that is not true.  IN can be used in expressions that do not involve sets (though the target of the IN must be enumerable.)

This is documented, for instance: https://www.freepascal.org/docs-html/ref/refsu59.html
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 670
  • Compiler Developer
Re: Why this is not valable on FPC
« Reply #6 on: November 09, 2019, 10:49:49 am »
That in is however not the same. marcov meant the operator in. The in of the forin loop is a fixed syntactic element however, just like to and downto for the ordinary for loop.
It is however possible to overload the in operator.

440bx

  • Hero Member
  • *****
  • Posts: 1198
Re: Why this is not valable on FPC
« Reply #7 on: November 09, 2019, 11:31:50 am »
That in is however not the same.
Agreed.  The semantic value of "in" is not the same but, _syntactically_ it can be used with something other than a set.

using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 9176
Re: Why this is not valable on FPC
« Reply #8 on: November 09, 2019, 11:41:29 am »
Yes. I overload it all the time e.g.:
Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2. operator in (const a,b:string):boolean;
  3. begin
  4.   Result := pos(b,a)>0;
  5. end;
« Last Edit: November 09, 2019, 12:33:17 pm by Thaddy »
also related to equus asinus.