Forum > General

[Solved] How to store data in Helper class

(1/3) > >>

heejit:
I need to extend TStringGrid with Helper class to add some procedure
this procedure need to store some data (array of strings, array of integer).

Helper class not allowed to add field.

Please suggest how can I do this.

korba812:
You can not declare fields in class helper because class helper does not have instance.
https://www.freepascal.org/docs-html/ref/refse61.html

If you can not use your own TStringList class then You can use data declared outside class helper, for example global variable or use var params in function:
function TMyHelper.Foo(var IntArr: TIntegerArray; StrArr: TStringArray);

Thaddy:
You can actually store data with type helpers but it needs a trick:Assignable typed constants in a function body that also takes a var parameter and the same result.
But I doubt this is what he means. The best way is still to derive a new stringgrid from Tstringgrid. You can also setup an external data store in the afterconstruction event and release it in the beforedestruction event. There are many options.

howardpc:
Here's a small example of subclassing TStringGrid as Thaddy suggests which you can adapt.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  uses Grids;   TExStringGrid = class(TCustomStringGrid)  private  const    arrayIncrement = 64;  private    FHighIntIndex: Integer;    FHighStrIndex: Integer;    IntArray: TBoundArray;    StrArray: TStringArray;  public    constructor Create(AOwner: TComponent); override;    function AddInt(anInt: SizeInt): Integer;    function IntExists(anInt: SizeInt; out anIndex: Integer): Boolean    // other methods you need ...  published    // publish whatever you need here  end; implementation constructor TExStringGrid.Create(AOwner: TComponent);begin  inherited Create(AOwner);  SetLength(IntArray, arrayIncrement);  SetLength(StrArray, arrayIncrement);  FHighIntIndex := -1;  FHighStrIndex := -1;end; function TExStringGrid.AddInt(anInt: SizeInt): Integer;var  dummy: Integer;begin  if IntExists(anInt, dummy) then    Exit(dummy);  Inc(FHighIntIndex);  if FHighIntIndex > High(IntArray) then    SetLength(IntArray, Length(IntArray) + arrayIncrement);  Result := FHighIntIndex;  IntArray[Result] := anInt;end; function TExStringGrid.IntExists(anInt: SizeInt; out anIndex: Integer): Boolean;var  i: Integer;begin  for i := 0 to FHighIntIndex do    if IntArray[i] = anInt then begin      anIndex := i;      Exit(True);    end;  anIndex := -1;  Result := False;end;        

heejit:
Yes extending the TStringGrid is good option

How I can use it in my Form

Navigation

[0] Message Index

[#] Next page

Go to full version