We help IT Professionals succeed at work.
Get Started

Getting property type

Marco Gasi
Marco Gasi asked
Last Modified: 2014-05-18
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

Open in new window

The SaveData procedure in the component is as folllows:

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

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.
Watch Question
Software architect
Top Expert 2012
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE