Recent

Author Topic: [Solved] How to store data in Helper class  (Read 3571 times)

heejit

  • Full Member
  • ***
  • Posts: 245
[Solved] How to store data in Helper class
« on: October 13, 2018, 12:56:06 pm »
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.
« Last Edit: October 13, 2018, 06:19:01 pm by jshah »

korba812

  • Sr. Member
  • ****
  • Posts: 390
Re: How to store data in Helper class
« Reply #1 on: October 13, 2018, 01:25:44 pm »
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

  • Hero Member
  • *****
  • Posts: 14164
  • Probably until I exterminate Putin.
Re: How to store data in Helper class
« Reply #2 on: October 13, 2018, 03:04:15 pm »
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.
Specialize a type, not a var.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to store data in Helper class
« Reply #3 on: October 13, 2018, 03:24:29 pm »
Here's a small example of subclassing TStringGrid as Thaddy suggests which you can adapt.
Code: Pascal  [Select][+][-]
  1.   uses Grids;
  2.  
  3.   TExStringGrid = class(TCustomStringGrid)
  4.   private
  5.   const
  6.     arrayIncrement = 64;
  7.   private
  8.     FHighIntIndex: Integer;    FHighStrIndex: Integer;
  9.     IntArray: TBoundArray;
  10.     StrArray: TStringArray;
  11.   public
  12.     constructor Create(AOwner: TComponent); override;
  13.     function AddInt(anInt: SizeInt): Integer;
  14.     function IntExists(anInt: SizeInt; out anIndex: Integer): Boolean
  15.     // other methods you need ...
  16.   published
  17.     // publish whatever you need here
  18.   end;
  19.  
  20. implementation
  21.  
  22. constructor TExStringGrid.Create(AOwner: TComponent);
  23. begin
  24.   inherited Create(AOwner);
  25.   SetLength(IntArray, arrayIncrement);
  26.   SetLength(StrArray, arrayIncrement);
  27.   FHighIntIndex := -1;  FHighStrIndex := -1;
  28. end;
  29.  
  30. function TExStringGrid.AddInt(anInt: SizeInt): Integer;
  31. var
  32.   dummy: Integer;
  33. begin
  34.   if IntExists(anInt, dummy) then
  35.     Exit(dummy);
  36.   Inc(FHighIntIndex);
  37.   if FHighIntIndex > High(IntArray) then
  38.     SetLength(IntArray, Length(IntArray) + arrayIncrement);
  39.   Result := FHighIntIndex;
  40.   IntArray[Result] := anInt;
  41. end;
  42.  
  43. function TExStringGrid.IntExists(anInt: SizeInt; out anIndex: Integer): Boolean;
  44. var
  45.   i: Integer;
  46. begin
  47.   for i := 0 to FHighIntIndex do
  48.     if IntArray[i] = anInt then begin
  49.       anIndex := i;
  50.       Exit(True);
  51.     end;
  52.   anIndex := -1;
  53.   Result := False;
  54. end;      
  55.  

heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to store data in Helper class
« Reply #4 on: October 13, 2018, 03:50:54 pm »
Yes extending the TStringGrid is good option

How I can use it in my Form

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to store data in Helper class
« Reply #5 on: October 13, 2018, 04:12:21 pm »
Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     procedure FormCreate(Sender: TObject);
  3.   private
  4.     FExStringGrid: TExStringGrid;
  5.   public
  6.  
  7.   end;
  8.  
  9. implementation
  10.  
  11. procedure TForm1.FormCreate(Sender: TObject);
  12. begin
  13.   FExStringGrid := TExStringGrid.Create(Self);
  14.   with FExStringGrid do begin
  15.     Align := alClient;
  16.     // set Options, RowCount, ColCount & other properties etc. as required
  17.     Parent := Self;
  18.   end;
  19. end;
« Last Edit: October 13, 2018, 04:40:01 pm by howardpc »

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: How to store data in Helper class
« Reply #6 on: October 13, 2018, 05:07:36 pm »
How I can use it in my Form
Like Howard showed you.

But you can also put a (clone)-declaration above the Form1. In that case you can just drop a normal TStringGrid on your form with which you can interact but the actual class is the one declared above the form.

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   TStringGrid = class(Grids.TStringGrid)
  4.   public
  5.     myNewVariable: integer;
  6.     // etc. etc.
  7.   end;
  8.  
  9.   { TForm1 }
  10.  
  11.   TForm1 = class(TForm)
  12.     StringGrid1: TStringGrid;
  13.   private
  14.  
  15.   public
  16.  
  17.   end;

heejit

  • Full Member
  • ***
  • Posts: 245
Re: How to store data in Helper class
« Reply #7 on: October 13, 2018, 06:18:45 pm »
I have used  unit level variable and class helper

So for each control/widget I will create a separate unit of helper


PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: How to store data in Helper class
« Reply #8 on: October 15, 2018, 01:43:08 pm »
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.
In that case you should also mention local, writeable, typed constants are merely scoped global variables so that no one is confused if they use this with multiple instances.

Thaddy

  • Hero Member
  • *****
  • Posts: 14164
  • Probably until I exterminate Putin.
Re: How to store data in Helper class
« Reply #9 on: October 15, 2018, 02:34:26 pm »
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.
In that case you should also mention local, writeable, typed constants are merely scoped global variables so that no one is confused if they use this with multiple instances.
Hence I wrote with instead of in. But you are correct. I should have mentioned that. (I almost always mention it when I refer to {$J+} elsewhere)
Specialize a type, not a var.

heejit

  • Full Member
  • ***
  • Posts: 245
Re: [Solved] How to store data in Helper class
« Reply #10 on: October 15, 2018, 05:29:44 pm »
Finally I created Normal Class instead of Helper and Pass the Control to the class
for further processing.

I needed multiple instance of class in one form and unit level variable was shared to
all instance that is why I have to go for Normal Class to isolate the variable to that instance only.




heejit

  • Full Member
  • ***
  • Posts: 245
Re: [Solved] How to store data in Helper class
« Reply #11 on: October 17, 2018, 03:16:51 pm »
Simple class not working for me.

So Now I  extended the existing control and add procedure  etc to it
and follow the @rvk Hack to create clone declaration of control for now it is working.

This give me BOTH the design time help for layout of control and customization also.

 

TinyPortal © 2005-2018