Recent

Author Topic: Is there a way to test if an object has a property and read or write to it?  (Read 2761 times)

vfclists

  • Hero Member
  • *****
  • Posts: 825
    • HowTos Considered Harmful?
Is there a way to test if an object has a property and read or write to it?

I am trying to share the TDataset event handling code of some data modules which may or may not have some properties. When the TDataset handling is processed it checks the owner (ie the data module) of the TDataset for some properties and their values and executes some code if those properties are present.

1. Is it possible to check just the property by its name as a string alone and make some changes? If the property is a simple type like a character or a string is it possible to use the string name to test for its existence and read or write to it? The idea is that it shouldn't be necessary to include the unit of the data module whose events are being set. There is no need to know the type in advance.

2. Another option I am thinking of is a more complex property whose unit is included in module with the event handling code. So that if the calling TDataset's owner has that property then the test can be made and the necessary code executed. I am not familiar with interfaces but that seems to be what I need here. Are they approriate for this purpose.

Windows - Lazarus 1.0.8/FPC 2.6.0
Linux - Lazarus 1.4.4 /FPC 2.6.4

howardpc

  • Hero Member
  • *****
  • Posts: 3210
Provided the property is published, and not of an esoteric type, Yes.
Include the typinfo unit in your uses clause.
Among many routines are:
IsPublishedProp() to see if a property is present,
and a variety of SetxxxProp and GetxxxProp functions (e.g. SetStrProp())
for manipulating property values via code.

vfclists

  • Hero Member
  • *****
  • Posts: 825
    • HowTos Considered Harmful?
Provided the property is published, and not of an esoteric type, Yes.
Include the typinfo unit in your uses clause.
Among many routines are:
IsPublishedProp() to see if a property is present,
and a variety of SetxxxProp and GetxxxProp functions (e.g. SetStrProp())
for manipulating property values via code.


It is a property I am adding to a regular data module. I am not subclassing it and making it into a new component.

Does the difference between public and published apply here, as I think what I am doing makes it a public property.
Windows - Lazarus 1.0.8/FPC 2.6.0
Linux - Lazarus 1.4.4 /FPC 2.6.4

howardpc

  • Hero Member
  • *****
  • Posts: 3210
Providing you add the property to be examined to a published section of the datamodule, you can query at runtime if the property in question is present in the datamodule. Either declare an explicit published section to hold such properties, or add them at the very top [immediately after "class(TDataModule)" ] of the datamodule declaration - all properties placed there are published by default.
If you add a property which is only public, there is no way I know of to test for its presence at runtime.