Recent

Author Topic: TStringGrid THelperClass  (Read 548 times)

pmesquita

  • New Member
  • *
  • Posts: 17
TStringGrid THelperClass
« on: February 24, 2021, 04:05:29 am »
Ladies and Gentlemen.

In Delphi I have the TNextGrid component where if I want to read or write a value in an ACol / ARow cell I can use the path:
Code: Pascal  [Select][+][-]
  1. CellBy[ACol, ARow].AsBoolean: = False;

However in TStringGrid for me to do this I will have to:
Code: Pascal  [Select][+][-]
  1. Cells[ACol, ARow]:= StrToBool ('False');

Is there any way to apply a THelperClass to the Cells property? or THelperString to do the conversion or just rewriting the TStringGrid so that you can create the
Code: Pascal  [Select][+][-]
  1. CellsBy[ACol, ARow].As [String / Boolean / Numeric]
property

lucamar

  • Hero Member
  • *****
  • Posts: 3916
Re: TStringGrid THelperClass
« Reply #1 on: February 24, 2021, 04:46:05 am »
Is there any way to apply a THelperClass to the Cells property? or THelperString to do the conversion or just rewriting the TStringGrid so that you can create the CellsBy[ACol, ARow].As [String / Boolean / Numeric] property

Maybe you should read the docs about the type helpers in SysUtils. Most of what you want to do is probably already there and, if not, you can extend them at will by ... "subclassing"? (not the most accurate word in this context, I know).
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2989
  • Compiler Developer
Re: TStringGrid THelperClass
« Reply #2 on: February 24, 2021, 09:26:30 am »
Even if one can easily extend the TStringHelper to provide a AsBoolean property using helper inheritance this will not help (no pun intended :P ) in this specific case, because the string helper will operate on a temporary copy that is returned by the getter of the Cells property, thus any changes will not be reflected in the grid itself.

pmesquita

  • New Member
  • *
  • Posts: 17
Re: TStringGrid THelperClass
« Reply #3 on: February 24, 2021, 12:08:21 pm »
Even if one can easily extend the TStringHelper to provide a AsBoolean property using helper inheritance this will not help (no pun intended :P ) in this specific case, because the string helper will operate on a temporary copy that is returned by the getter of the Cells property, thus any changes will not be reflected in the grid itself.
So in this situation that I proposed I can only read the cell phone value through the getter and perform the conversion or other operation and not be using the setter?  To be able to do this, would I have to rewrite the cell access mode so that I would get the ACol object, ARow, for example and thus have a THelper associated with the object?

pmesquita

  • New Member
  • *
  • Posts: 17
Re: TStringGrid THelperClass
« Reply #4 on: February 24, 2021, 12:13:43 pm »
Is there any way to apply a THelperClass to the Cells property? or THelperString to do the conversion or just rewriting the TStringGrid so that you can create the CellsBy[ACol, ARow].As [String / Boolean / Numeric] property

Maybe you should read the docs about the type helpers in SysUtils. Most of what you want to do is probably already there and, if not, you can extend them at will by ... "subclassing"? (not the most accurate word in this context, I know).

Yes I read the documentation, but there is more or less what I need however I am wanting to find out how I can set a Boolean value on my cell phone without having to do BoolToStr every time .. It's easier to just type  ..
Code: Pascal  [Select][+][-]
  1. Cell [acol, ARow] .AsBoolean: = False;
is the subclass or THelper, finally, it would be in charge of performing the conversion to string or any other type ...

lucamar

  • Hero Member
  • *****
  • Posts: 3916
Re: TStringGrid THelperClass
« Reply #5 on: February 24, 2021, 01:44:37 pm »
[...] I am wanting to find out how I can set a Boolean value on my cell phone without having to do BoolToStr every time .. It's easier to just type  ..
Code: Pascal  [Select][+][-]
  1. Cell [acol, ARow] .AsBoolean: = False;
is the subclass or THelper, finally, it would be in charge of performing the conversion to string or any other type ...

That's what I meant: you should familiarize yourself with all the helpers already there. For example, you could do something like:
Code: Pascal  [Select][+][-]
  1. Cell [acol, ARow] := False.ToString(TUseBoolStrs.True);
using the Boolean type helper, though it's not very friendly, what with that scoped enum :(.

Maybe not exactly what you want but then, as PascalDragon said, you wouldn't be able to modify the Cell (or any other property, IIRC) directly with a helper unless it's a class helper for the grid.

The point is that SysUtils have helpers for most (or at least the "base") types which allow you to convert from one to another using that "easy" object notation you seem to prefer. :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2989
  • Compiler Developer
Re: TStringGrid THelperClass
« Reply #6 on: February 24, 2021, 01:58:39 pm »
Even if one can easily extend the TStringHelper to provide a AsBoolean property using helper inheritance this will not help (no pun intended :P ) in this specific case, because the string helper will operate on a temporary copy that is returned by the getter of the Cells property, thus any changes will not be reflected in the grid itself.
So in this situation that I proposed I can only read the cell phone value through the getter and perform the conversion or other operation and not be using the setter?  To be able to do this, would I have to rewrite the cell access mode so that I would get the ACol object, ARow, for example and thus have a THelper associated with the object?

You'd have to work directly on where the grid stores the strings as properties wouldn't help you there.

What you could do however would be to use a helper on the grid instead of the string (not tested, but should work):

Code: Pascal  [Select][+][-]
  1. type
  2.   TStringGridHelper = class helper for TStringGrid
  3.   private
  4.     function GetCellAsBool(aCol, aRow: Integer): Boolean;
  5.     procedure SetCellAsBool(aCol, aRow: Integer; aValue: Boolean);
  6.   public
  7.     property CellAsBool[Col, Row: Integer]: Boolean read GetCellAsBool write SetCellAsBool;
  8.   end;
  9.  
  10. function TStringGridHelper.GetCellAsBool(aCol, aRow: Integer): Boolean;
  11. begin
  12.   Result := StrToBool(Cells[aCol, aRow]);
  13. end;
  14.  
  15. procedure TStringGridHelper.SetCellAsBool(aCol, aRow: Integer; aValue: Boolean);
  16. begin
  17.   Cells[aCol, aRow] := BoolToStr(aValue, True);
  18. end;

jamie

  • Hero Member
  • *****
  • Posts: 4472
Re: TStringGrid THelperClass
« Reply #7 on: February 24, 2021, 11:24:38 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, lcltype,
  9.   Grids;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     StringGrid1: TStringGrid;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. operator := (R:Boolean):String;
  29. implementation
  30.  
  31. {$R *.lfm}
  32. Operator := (R:Boolean):String;
  33. Begin
  34.   Result :=R.ToString(TUseBoolStrs.True);
  35. end;
  36. Operator := (R:Boolean):TTranslateString;
  37. Begin
  38.   Result :=R.ToString(TUseBoolStrs.True);
  39. end;
  40. { TForm1 }
  41.  
  42. procedure TForm1.Button1Click(Sender: TObject);
  43. begin
  44.  Caption:= False;
  45.  StringGrid1.Cells[0,0] :=True;
  46. end;
  47.  
  48. end.
  49.  
  50.  

My Version of it, and it actually WORKS!  :D
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 3916
Re: TStringGrid THelperClass
« Reply #8 on: February 24, 2021, 11:51:32 pm »
My Version of it, and it actually WORKS!  :D

Ouch! We got stuck on the "helper" thing and forgot about operators :-[
Well done, jamie!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018