...or classes that have been compiled with {$M+}...
-Andreas
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:owner-delphi@;delphi.org.nz]On
Behalf Of Kyley Harris
Sent: Friday, 25 October 2002 16:58
To: Multiple recipients of list delphi
Subject: RE: [DUG]: Polymorphic assignment to Color - THANKS + New Info
Hey Giovanni, RTTI info relies on you subclassing from Tpersistent. This
is how Tfont etc all work also.
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:owner-delphi@;delphi.org.nz] On
Behalf Of Moretti, Giovanni
Sent: Friday, 25 October 2002 4:15 p.m.
To: Multiple recipients of list delphi
Subject: RE: [DUG]: Polymorphic assignment to Color - THANKS + New Info
Hi
Thanks to you all for your help and the tutorial on interfaces which was
very handy as I'm still wrapping my mind around them (apart from this
problem).
I decided to use Kyley's RTTI interrogation technique - I understand
what it's doing whereas my OO development is still digesting interfaces.
In due course I may go the interface way, but for the moment, the RTTI
method will suffice.
I decided to check the functions worked as I expected by creating my own
object (rather than using Form/Panel, Font background ...)
I defined a minimal object with get/set properties and tried to use it
(see skeletal code below). Compiled fine but gave a "Doesn't have a
Colour Property" message, which means that IsPublishedProp is returning
nil.
After wandering around the help system on RTTI I went home and the
answer (well a solution anyway) came while I was driving. I was
pondering on why the system-defined objects were fine and mine wasn't.
Answer - they're all derived from TComponent. Even though the property
is public and published, for "IsPublishedProp()" to work, the class
(apparently) has to be a descendent of TComponent, TObject won't work.
So now I have a method I can use for both VCL objects and those I
create.
I greatly appreciate your help
Thank you all
Giovanni
CompSci - Massey Uni
==========================================================
// TMyColourSetTester = class(TObject) <= FAILS AT RUNTIME
TMyColourSetTester = class(TComponent) <= WORKS FINE
private
MyColour : TColor;
function GetCol : TColor;
procedure SetCol (value : TColor);
public
constructor create; // just calls inherited create
published
property
color : TColor read GetCol write SetCol;
end;
procedure SetObjectColour (name : string;
AObject : TObject;
newColour : TColor);
var result : boolean;
propinfo : PPropInfo;
classTypeInfo : PTypeInfo;
begin
result := IsPublishedProp(AObject,'color');
If result then SetOrdProp(AObject,'Color',newColour)
else showmessage(name+' doesn''t have a Color property'); end;
var Coltester : TMyColourSetTester;
ColTester:= TMyColourSetTester.create;
ColTester.color:= clYellow; // Make sure it goes
setObjectColour('ColTester', ColTester, clAqua);
==========================================================
------------------------------------------------------------------------
---
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/