Avatar of Marco Gasi
Marco Gasi
Flag for Spain asked on

Getting property type

Hi all.

I'm building a small class I wnat to use to save session data for a couple of my programs I use frequently.
The goal of the class is to make easier store in a ini file data uswed by program to do specific job. To make it more clear: I have a program I use to make a backup of some project to a backup folder. Since I use for several project, I need the functionality to remember each project's settings (source folder, destination folder, if copy recursively and so on).

Since I have the same need in another program, I decided to develop a class or a component which make this easy: I want to pass a list of components properties:
  with mgProgramDataManager1.PropertiesValues do
  begin
    Add('edtProp.Text');
    Add('mmoProp.Lines');
    Add('cbxItems.Items');
    Add('cbxItems.ItemIndex');
    Add('cbxProp1.Checked');
    Add('cbxProp2.Checked');
    Add('rbt1.Checked');
    Add('rbt2.Checked');
  end;

Open in new window


The SaveData procedure in the component is as folllows:

procedure TmgProgramDataManager.SaveData(AJobName: string);
var
  I: Integer;
  Cmp: TComponent;
  sl: TStringDynArray;
  PropInfo: PPropInfo;
begin
  FFileName := AddBackSlash(FFilePath) + 'jobs.ini';
  FIni := TIniFile.Create(FFileName);
  for I := 0 to FPropertiesValues.Count - 1 do
  begin
    sl := SplitString(FPropertiesValues[I], '.');
    Cmp := FOwner.FindComponent(sl[0]);
    PropInfo := GetPropInfo(Cmp.ClassInfo, sl[1]);
    FIni.WriteString(AJobName, Cmp.Name, GetPropValue(Cmp, PropInfo.Name));
  end;
  ShowMessage('job saved');
end;

Open in new window


But not all works fine: properties like Lines is stored as a number. So I think to have check if a property is of type TStrings and change the code accordingly, but I can't get rid of how I can do it.

I think to have to use RTTI, but I'm struggled trying to understand how to get the property type.

I also know about component streaming and I had yet built a working class, but  I had two problems:
1) as it is, component streaming use default values, so if in a job a checkbox is checked and in another one the checkbox is not checked and I want to pas from the first job to the second one, all values are restored as they were, but the checkbox remains checked because saving the second job the unchecked state is not saved into the stream file (so it can't be restored)
2) I solved this problem checking by myself is a component is a TCheckBox or a TRadioButton but in this case the component will not be able to manage any descendant component (for instance TJvCheckBox)or it will have to create an if statement for any possible component like TCheckListBox an so on.

So the question is: how can I get the type of a property to know if it is TStrings? Or, alternatively, how can deal with this type of task?

Thanks in advance.
Marco
Delphi

Avatar of undefined
Last Comment
Sinisa Vuk

8/22/2022 - Mon
Sinisa Vuk

If you test cmp if it is stringlist:
...
if cmp.InheritsFrom(TStringList) then
begin
  FIni.WriteString(..., ..., (cmp as TStringList).DelimitedText);
end
else if cmp.InheritsFrom(TCheckBox) then //this will cover TJvCheckBox, TCheckBox..
begin
  FIni.WriteBool(..., ..., (cmp as TCheckBox).Checked);
end
else ...
   //other components
...

Open in new window


... then you can save stringlist as one single row. You can save type of value (boolean, stringlist, ...)
Marco Gasi

ASKER
Hi Sinisa. Thank you for being always ready to go with my questions :-)

Perfect solution for direct properties but I see a problem: if Cmp is, let say, a TMemo, it doesn't inherit from TStringList nor from TStrings. I should test not for TMemo but for TMemo.Lines. Now, from my main app I pass 'mmoProp.Lines' and my class detects the component type:
    sl := SplitString(FPropertiesValues[I], '.'); //'mmoProp.Lines' is splitted in 'mmoProp' and 'Lines'
    Cmp := FOwner.FindComponent(sl[0]); // Cmp is mmoProp

Open in new window

And following code writes the correct value for let say Checkbox1.checked or edit1.text

    PropInfo := GetPropInfo(Cmp.ClassInfo, sl[1]);
    FIni.WriteString(AJobName, Cmp.Name, GetPropValue(Cmp, PropInfo.Name));

Open in new window


But for Lines or Items it writes a number, so I should check the second part of splitted string and I'm wondering if there is a more correct solution than an infinite serie of
if sl[1] = 'Lines'
else
if sl[1] = 'Items'

Open in new window


to manage this.
Geert G

have you checked gexperts.org ?
in the tool, there is a backup project option

also from the website, you can download the complete source code for the delphi plugin
it's rather extensive ... and complicated code ... but it's all in there
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Marco Gasi

ASKER
Hi Geert, Thanks for the suggestion. But I'm not doing a backup of the project files. I'm trying to make my app can remember its settings: I need to write somehow somewhere the values of several controls (like checkboxes, listboxes, comboboxes and so on) linking all of these values with a name the user can use to restore those values. I don't know if code to backup project files can help me in this...
ASKER CERTIFIED SOLUTION
Sinisa Vuk

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Marco Gasi

ASKER
Wow, Sinisa, a lot of code to study: thank you very very mutch!  I thouhgt you had abandoned this question and I'm happy you didn't it.
As I can understand at the first look, that's exactly what I was looking for. Thenk you again

Marco
Marco Gasi

ASKER
Thank you!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Sinisa Vuk

I'm glad to help.