Board index » delphi » Setting Property name as property value

Setting Property name as property value

Hello All,
                I have just created a new component with a series of properties
with a string value.Is it possible to assign
a property name to a property value by using a  'For loop'.
My property names are inch1 to inch10.
I want the Property Name to equal the string b, and then i want to set its value.

Example:-

procedure TForm1.Button1Click(Sender: TObject);
var a: integer;
    b: string;
begin

     for a := 1 to 10 do
     b := 'Inch' + String(a);
     MyBookClass1.( Property Name) := String (a);

end;

Hope that you can help
                                        Mike Geraghty.

 

Re:Setting Property name as property value


Hello All,
                I have just created a new component with a series of properties
with a string value.Is it possible to assign
a property name to a property value by using a  'For loop'.
My property names are inch1 to inch10.
I want the Property Name to equal the string b, and then i want to set its value equal to the property name.

Example:-

procedure TForm1.Button1Click(Sender: TObject);
var a: integer;
    b: string;
begin

     for a := 1 to 10 do
     b := 'Inch' + String(a);
     MyBookClass1.( Property Name) := b;

end;

Hope that you can help
                                        Mike Geraghty.

Re:Setting Property name as property value


Please only post PLAIN TEXT messages !

What you need is called RTTI (runtime type information), e.g.

uses
  TypInfo;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  P: PPropInfo;
begin
  for i := 1 to 10 do
  begin
    P := GetPropInfo(TMyBookClass.ClassInfo, 'Inch' + IntToStr(i));
    if Assigned(P) and (P.PropType^.Kind = tkString) then
      SetStrProp(MyBookClass1, P, IntToStr(i));
  end;
end;

Quote
"orion45" <orio...@ic24.net> wrote in message

news:u6g8lg1fb4dfe1@corp.supernews.com...
Hello All,
                I have just created a new component with a series of
properties
with a string value.Is it possible to assign
a property name to a property value by using a  'For loop'.
My property names are inch1 to inch10.
I want the Property Name to equal the string b, and then i want to set its
value.

Example:-

procedure TForm1.Button1Click(Sender: TObject);
var a: integer;
    b: string;
begin

     for a := 1 to 10 do
     b := 'Inch' + String(a);
     MyBookClass1.( Property Name) := String (a);

end;

Hope that you can help
                                        Mike Geraghty.

Re:Setting Property name as property value


Many Thanks,
                       It Worked and i did not even need the line marked
with the asterix below.

                              Mike Geraghty.

Quote
"M.H. Avegaart" <avega...@NOSPAMmccomm.nl> wrote in message

news:a498sk$1beb$1@scavenger.euro.net...
Quote
> Please only post PLAIN TEXT messages !

> What you need is called RTTI (runtime type information), e.g.

> uses
>   TypInfo;

> procedure TForm1.Button1Click(Sender: TObject);
> var
>   i: Integer;
>   P: PPropInfo;
> begin
>   for i := 1 to 10 do
>   begin
>     P := GetPropInfo(TMyBookClass.ClassInfo, 'Inch' + IntToStr(i));

** >     if Assigned(P) and (P.PropType^.Kind = tkString) then
Quote
>       SetStrProp(MyBookClass1, P, IntToStr(i));
>   end;
> end;

> "orion45" <orio...@ic24.net> wrote in message
> news:u6g8lg1fb4dfe1@corp.supernews.com...
> Hello All,
>                 I have just created a new component with a series of
> properties
> with a string value.Is it possible to assign
> a property name to a property value by using a  'For loop'.
> My property names are inch1 to inch10.
> I want the Property Name to equal the string b, and then i want to set its
> value.

> Example:-

> procedure TForm1.Button1Click(Sender: TObject);
> var a: integer;
>     b: string;
> begin

>      for a := 1 to 10 do
>      b := 'Inch' + String(a);
>      MyBookClass1.( Property Name) := String (a);

> end;

> Hope that you can help
>                                         Mike Geraghty.

Other Threads