Link to home
Create AccountLog in
Avatar of Cheng_sam
Cheng_sam

asked on

RTTI Tobject

i want to set a my own object property in runtime that descended from Tobject. I  use RTTI method but fail.   My code is below. thanks.

type
   Tmyobject=class(Tobject)
      Private
       fmyproperty:integer;  
       procedure   Setmyproperty(Value:Integer);
     published
           property myproperty : Integer read Fmyproperty write Setmyproperty default 1;//myfont:tfont;
     end;

procedure Tmyobject.Setmyproperty (Value: integer);
begin
     Fmyproperty := Value;
end;
procedure TfrmMain.Button4Click(Sender: TObject);
var
  p: PPropInfo;testobject:Tmyobject;
 begin
   testobject:=Tmyobject.Create;
   testobject.myproperty :=400;
   p := GetPropInfo(testobject.ClassInfo , 'myproperty');
  if Assigned(p) then
    SetOrdProp(testobject, p, 100);
    showmessage('testobject='+inttostr(testobject.myproperty));
  end;
SOLUTION
Avatar of dinilud
dinilud
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Cheng_sam
Cheng_sam

ASKER

it works. but it need more memory(double) and more time(double).
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
it works fine .    it seems O.K in exe size . Thanks  ciuly and dinilud.
>> it seems O.K in exe size .
you won't feel the difference because all components (actually all descendents from TPersistent) have the $M+ ;)
you will however see the diferrence when you are coding non-vcl applications.
True. Better derive from TPersistent ??
not necessarely. you don't always need the overhead of TPersistent (sure, it's not big, but when dealing with large amount of data the difference can be seen.

I usually preffer for data wrappers to use a base class that just has the $M+/- around it.