Recent

Author Topic: Is there a better way?  (Read 1528 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Is there a better way?
« on: December 07, 2020, 03:36:15 am »
I have a fixed array THEPIC[0..19] of Boolean; I have a form with 20 labels named LX0..LX19.

 I want a procedure to update the Array with the Boolean value and also the corresponding Labeln.Caption.

The following will work but was wondering if there isn't a better way.

Thanks


Code: Pascal  [Select][+][-]
  1.  procedure TForm1.SetThePic(APICK : Integer; AVALUE : Boolean);
  2.   Var S : String;
  3.   begin
  4.    S := BoolToStr(AVALUE, False);
  5.    if APICK = 0 then LX0.Caption := S  
  6.    else if APICK = 1 then LX1.Caption := S
  7.    else if APICK = 2 then LX0.Caption := S  
  8.    else if APICK = 3 then LX1.Caption := S
  9.    else if APICK = 4 then LX0.Caption := S  
  10.    else if APICK = 5 then LX1.Caption := S
  11.    else if APICK = 6 then LX0.Caption := S  
  12.    else if APICK = 7 then LX1.Caption := S
  13.    else if APICK = 8 then LX1.Caption := S
  14.    else if APICK = 9 then LX0.Caption := S  
  15.    else if APICK = 10 then LX1.Caption := S
  16.    else if APICK = 11 then LX0.Caption := S  
  17.    else if APICK = 12 then LX1.Caption := S
  18.    else if APICK = 13 then LX0.Caption := S  
  19.    else if APICK = 14 then LX1.Caption := S
  20.    else if APICK = 15 then LX0.Caption := S  
  21.    else if APICK = 16 then LX1.Caption := S
  22.    else if APICK = 17 then LX0.Caption := S  
  23.    else if APICK = 18 then LX1.Caption := S
  24.    else if APICK = 19 then LX0.Caption := S;  
  25.    
  26.    THEPICK[APIC] := AVALUE;
  27.    
  28.    end;
  29.    
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

egsuh

  • Hero Member
  • *****
  • Posts: 1292
Re: Is there a better way?
« Reply #1 on: December 07, 2020, 04:52:31 am »
I don't know whether this is better way or not, but I use in following way.


Code: Pascal  [Select][+][-]
  1. var
  2.      FFlags:  longint;
  3.  
  4. property AFlag(i: integer): boolean read getFlag write setFlag;
  5.  
  6. function getFlag(i:integer): boolean;
  7. begin
  8.     Result := (FFlags and (2**(i+1))) <> 0;  // i+1 assumes you do not use 0.
  9. end;
  10.  
  11. procedure setFlag(I:integer; AValue:Boolean);
  12. begin
  13.      if AValue then FFlags := FFlags or (2 **(i+1))
  14.                    else FFlags := FFlags and (not (2 **(i+1)));
  15. end;  
  16.  
   

The max integer will depend on the size of longint. If it is 64bits long, you can define up to 64.
Of course you have to initialize FFlags somewhere.

Then you can use like:

    AFlag[32] := true;
    AFlag[22] := false;
 
    if AFlag[15] then.....
« Last Edit: December 07, 2020, 05:00:54 am by egsuh »

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: Is there a better way?
« Reply #2 on: December 07, 2020, 05:09:52 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SetThePic(APick: Integer; AValue: Boolean);
  2. begin
  3.   (FindComponent('LX%d'.Format(APick mod 2)) as TLabel).Caption := BoolToStr(AValue, False);
  4.   // do other stuff...
  5. end;
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Is there a better way?
« Reply #3 on: December 07, 2020, 05:50:33 am »
@furous programer

 I can't get it to compile. And I have to say I don't understand it.

unit1.pas(1178,44) Error: Incompatible type for arg no. 1: Got "Int64", expected "Array Of Const"


   (FindComponent('LX%d'.Format(APick mod 2)) as TLabel).Caption := BoolToStr(AValue, False);
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

egsuh

  • Hero Member
  • *****
  • Posts: 1292
Re: Is there a better way?
« Reply #4 on: December 07, 2020, 05:56:52 am »
If your interest is on declaring LX0 or LX1, you may use like:
 
   if APICK in [0,2,4,6,9,11,13,15,17,19]
     then LX0.Caption := S
     else  LX1.Caption := S;

APICK should be 0..255 in this case.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Is there a better way?
« Reply #5 on: December 07, 2020, 06:06:12 am »
No I don't think that is what I'm after.

I have 21 TEdits on a screen and an array of boolean.

I wanted a procedure where I pass in an index of the array and a value of true or false.

The procedure would TheArray[IDX] := 'True'  or 'False];

And then update the caption of the TLabel. The TLavel's are named LX1..LX20.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

bytebites

  • Hero Member
  • *****
  • Posts: 640
Re: Is there a better way?
« Reply #6 on: December 07, 2020, 06:23:51 am »
And how is your explanation related to the code in the first post? 

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Is there a better way?
« Reply #7 on: December 07, 2020, 06:45:34 am »
Sorry It's getting late here.

I have a Form with 21 TLabels I also have a fixed array of ThePic[0..21] of Boolean.

The Labels are named LX0 .. LX1.

I would like to call a procedure passing the number 0..20 representing the Label and and a boolean value.

The procedure would convert the boolean value to a string 'True' or 'False' and update the on Form correct Labes, LX0 ..LX20 caption to true of false.

 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

bytebites

  • Hero Member
  • *****
  • Posts: 640
Re: Is there a better way?
« Reply #8 on: December 07, 2020, 07:09:21 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SetThePic(APick: Integer; AValue: Boolean);
  2. begin
  3.   (FindComponent('LX%d'.Format(APick mod 2)) as TLabel).Caption := BoolToStr(AValue, False);
  4.   // do other stuff...
  5. end;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SetThePic(APick: Integer; AValue: Boolean);
  2. begin
  3.   (FindComponent('LX%d'.Format([APick mod 2)]) as TLabel).Caption := BoolToStr(AValue, False);
  4.   // do other stuff...
  5. end;
  6.  

speter

  • Sr. Member
  • ****
  • Posts: 349
Re: Is there a better way?
« Reply #9 on: December 07, 2020, 07:26:20 am »
Code: [Select]
...Format([APick mod 2)]) as TLabel...
I think that should be
Code: [Select]
...Format([APick mod 2])) as TLabel...
cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Is there a better way?
« Reply #10 on: December 07, 2020, 07:40:53 am »
@Spter
That compiles.

I'll text it.
Thanks
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

egsuh

  • Hero Member
  • *****
  • Posts: 1292
Re: Is there a better way?
« Reply #11 on: December 07, 2020, 07:43:33 am »
You are confusing us. I think you have 22 labels named LX0, LX1, LX2, ... LX21.

You should write:

      (FindComponent(Format('LX%d',  [APick])) as TLabel).Caption := BoolToStr(AValue, False);

But I don't think finding component by name is a good practice. You'd better define :

      Labels: array of TLabel;
      // and somewhere, like TForm.FormCreate
      Labels := [LX0, LX1, LX2, ... LX21];

And then call
     Labels[APick].Caption := BoolToStr(AValue, False);
« Last Edit: December 07, 2020, 07:52:23 am by egsuh »

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Is there a better way?
« Reply #12 on: December 07, 2020, 08:31:04 am »
No I have 21.

As for confusing you Yea, probably.
When you look at the screenshot the L0 .. L20 correspond to the 0 .. 20 numbers on the right just to the left and under the GO button. The '-----' are the label's LX0..LX20.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

egsuh

  • Hero Member
  • *****
  • Posts: 1292
Re: Is there a better way?
« Reply #13 on: December 07, 2020, 08:57:30 am »
Well it's not important whether there are 21 or 22 labels.  Anyway I can think of following way (Still not sure whether this is better or not).

Code: Pascal  [Select][+][-]
  1. TForm1 = class(TForm)
  2.     //....... LX0..LX20 would be defined here
  3. private
  4.     Picks : array [0..20] of Boolean;  
  5.     LXes: array of TLabel;
  6.  
  7.     function getPick(i: integer): boolean;
  8.     function setPick(i:integer; AValue:Boolean);
  9.  
  10. public
  11.     property Pick [i: integer] : Boolean read getPick write setPick;  
  12. end;
  13.  
  14. implementation
  15.  
  16. procedure TForm1.FormCreate(Sender: TObject);
  17. var
  18.      i : integer;  
  19. begin
  20.      LXes := [LX0, LX1, LX2, .. . LX20];
  21.      for i := Low(Picks) to High(Picks) do Picks[i] := False;
  22. end;
  23.  
  24.  
  25. function TForm1.getPick(i:integer): boolean;
  26. begin
  27.     Result := Picks[i];
  28. end;
  29.  
  30. procedure TForm1.setPick (I:integer; AValue:Boolean);
  31. begin
  32.      Picks[i] := AValue;
  33.      LXes[i].Caption := BoolToStr(AValue, false);
  34. end;  
  35.  

 

TinyPortal © 2005-2018