Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RTTI - GetProp not working :(

83 views
Skip to first unread message

Nikola Janković

unread,
Jan 21, 2006, 1:21:20 PM1/21/06
to
I sincerely hoped that something like:

procedure TBaseObjList.FilterByDate(propName: string; startDate, endDate:
TDateTime);
var
i: Integer;
prop: TDateTime;
begin
for i:= Count-1 downto 0 do
begin
prop:= TDateTime(GetObjectProp(TBaseObj(Items[i]),propName));
if (prop < startDate) or (prop > endDate) then
Remove(TBaseObj(Items[i]));
end;
end;

procedure TBaseObjList.FilterByProperty(propName: string; propValue: string);
var
i: Integer;
begin
for i:= Count-1 downto 0 do
begin
if GetStrProp(TBaseObj(Items[i]),propName) <> propValue then
Remove(TBaseObj(Items[i]));
end;
end;

would work, but it doesn't. I get EPropertyError that property does not exist,
although it does and it is published (in second example), and incompatible types
(in first).

Please help me make it work.

Thanks in advance
nikola

danny heijl

unread,
Jan 21, 2006, 3:36:11 PM1/21/06
to
Nikola Janković schreef:

> would work, but it doesn't. I get EPropertyError that property does not
> exist, although it does and it is published (in second example), and
> incompatible types (in first).

TDateTime is not an object, it's just typedef for a double.

Danny
---

Rob Kennedy

unread,
Jan 21, 2006, 3:40:26 PM1/21/06
to
Nikola Janković wrote:
> procedure TBaseObjList.FilterByDate(propName: string; startDate, endDate:
> TDateTime);
> var
> i: Integer;
> prop: TDateTime;
> begin
> for i:= Count-1 downto 0 do
> begin
> prop:= TDateTime(GetObjectProp(TBaseObj(Items[i]),propName));

GetObjectProp returns a TObject, not a TDateTime. TDateTime is a
floating-point type, so use GetFloatProp.

> if (prop < startDate) or (prop > endDate) then
> Remove(TBaseObj(Items[i]));
> end;
> end;
>
> procedure TBaseObjList.FilterByProperty(propName: string; propValue:
> string);
> var
> i: Integer;
> begin
> for i:= Count-1 downto 0 do
> begin
> if GetStrProp(TBaseObj(Items[i]),propName) <> propValue then
> Remove(TBaseObj(Items[i]));
> end;
> end;
>
> would work, but it doesn't. I get EPropertyError that property does not
> exist, although it does and it is published (in second example), and
> incompatible types (in first).

What is the ancestor of TBaseObj? A property isn't really published
unless the class was compiled in the $M+ state, or if one of its
ancestors was compiled in that state. If your class descends (directly
or indirectly) from TPersistent, then it has RTTI. Otherwise, it doesn't.

If your class doesn't descend from TPersistent, then your class
declaration needs to look like this:

type
{$M+}
TBaseObj = class
end;
{$M-}

That turns on RTTI for the class.

--
Rob

Nikola Janković

unread,
Jan 21, 2006, 4:37:41 PM1/21/06
to
Rob Kennedy wrote:
> Nikola Janković wrote:
>
>...

>
> GetObjectProp returns a TObject, not a TDateTime. TDateTime is a
> floating-point type, so use GetFloatProp.

It was obvious for someone able to think :)


>
> What is the ancestor of TBaseObj? A property isn't really published
> unless the class was compiled in the $M+ state, or if one of its
> ancestors was compiled in that state. If your class descends (directly
> or indirectly) from TPersistent, then it has RTTI. Otherwise, it doesn't.
>
> If your class doesn't descend from TPersistent, then your class
> declaration needs to look like this:
>
> type
> {$M+}
> TBaseObj = class
> end;
> {$M-}
>
> That turns on RTTI for the class.
>

TBaseObj = class(TObject)
so this M directive did the trick.

Thanks.

Nikola Janković

unread,
Jan 21, 2006, 4:34:45 PM1/21/06
to

Working for about 20 hours straight, I seem to cannot think clearly anymore.

Thanks, that was so obvious. <blush>

0 new messages