Recent

Author Topic: iterate through variables in TObject (related to storing user preferences)  (Read 1683 times)

Eleazar

  • New Member
  • *
  • Posts: 26
Hello,

I am experimenting with methods to store user preferences (checked boxes, colors, etc.).

I also want them:
-easily accessible, -grouped and -‘viewable in code completion’

I came up with the following idea:
Create an object with variables.

TOptions=class(TObject)
    public
    UseTwoMonitors:boolean;
    LastPath:string;
    end;
In Formcreate:
gOptions:=TOptions.Create(self);

Now I can use it like:
if gOptions.UseTwoMonitors=True then ..

nothing special so far…

Now I want to store the variables with their names and values in, for example, an INI file.
For this I do not want to re-type the names, get the values and store it.

What I would like is to iterate through all variables, get the name and value, and store it.
Something like:
X=GetVariableCount  (like Componentcount)
In a for-next loop:
 Save  Variable
  • .name      save  variable.value

Next x

Is there a way to do this? Maybe using Classinfo?
Maybe it is very simple, but I can’t figure it out…

Thank you for your input!

Handoko

  • Hero Member
  • *****
  • Posts: 5150
  • My goal: build my own game engine using Lazarus
Hello Eleazar,
Welcome to the forum.

This code may give you some ideas:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   AComponent: TComponent;
  4.   S: string;
  5.   i: Integer;
  6. begin
  7.   S := '';
  8.   for i := 0 to (ComponentCount-1) do
  9.   begin
  10.     AComponent := Components[i];
  11. {
  12.     case AComponent.ClassType of
  13.       // do something based on its type
  14.     end;
  15. }
  16.     S := S + AComponent.Name + LineEnding;
  17.   end;
  18.   ShowMessage(S);
  19. end;

Note:
Pascal uses for-do-begin-end, without next. Next is for Basic and its variants.
http://wiki.freepascal.org/FOR..DO
« Last Edit: January 21, 2018, 05:24:26 pm by Handoko »

Eleazar

  • New Member
  • *
  • Posts: 26
Dear Handoko,

Thanks for your input. I had already tried something like that, but it will not show variables, only components...
Just after posting my question my thoughts wandered towards using a record instead of a TObject...
But I am still wondering about my first question...


Handoko

  • Hero Member
  • *****
  • Posts: 5150
  • My goal: build my own game engine using Lazarus
Sorry, I didn't read your post carefully. So did you meant you want to list all the variables in a class for saving and loading? Interesting. I haven't tried, I could be wrong but I think you may use TStream to do it. Something like this:
http://wiki.freepascal.org/Streaming_components
« Last Edit: January 21, 2018, 06:04:28 pm by Handoko »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
So you don't need to stream all properties of -say- checkbox but only property State, yes?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

taazz

  • Hero Member
  • *****
  • Posts: 5368
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Eleazar

  • New Member
  • *
  • Posts: 26
Handoko: I also briefly looked at streaming, but it looks rather complex. Moreover, the data is not ‘readable by humans’ I think, in contrast to an INI file.

Blaazen: “So you don't need to stream all properties of -say- checkbox but only property State, yes?”
Exactly, that is what I want.

I am now trying to work with a record.
This also allows me to keep using code-completion (one of my requirements)
Will post if it works.

Regards

taazz

  • Hero Member
  • *****
  • Posts: 5368
Handoko: I also briefly looked at streaming, but it looks rather complex. Moreover, the data is not ‘readable by humans’ I think, in contrast to an INI file.
What is so complex exactly? You only need to base your object on tpersistent and everything is taken care for you.

Blaazen: “So you don't need to stream all properties of -say- checkbox but only property State, yes?”
Exactly, that is what I want.

I am now trying to work with a record.
This also allows me to keep using code-completion (one of my requirements)
Will post if it works.

Regards
http://wiki.freepascal.org/Streaming_JSON
http://wiki.freepascal.org/TXMLPropStorage
« Last Edit: January 21, 2018, 07:09:51 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018