Marco Gasi
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:
The SaveData procedure in the component is as folllows:
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
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;
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;
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
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:
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
to manage this.
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
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));
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'
to manage this.
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
As I can understand at the first look, that's exactly what I was looking for. Thenk you again
Marco
ASKER
Thank you!
I'm glad to help.
Open in new window
... then you can save stringlist as one single row. You can save type of value (boolean, stringlist, ...)