Board index » delphi » Referencing objects with variables

Referencing objects with variables

Hi,

A simple one I'm afraid. I have a string with the name of an object and its
property, i.e.

var
  st: string
begin
  st := 'Form1.Caption';

I would like to use a list of variables like this to assign values from
another list. I tried typecasting

with (st as TObject) do
  st := 'New Caption';

But I'm missing something basic here. Can anyone help me out? Cheers.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

 

Re:Referencing objects with variables


Quote
>A simple one I'm afraid. I have a string with the name of an object and its
>property, i.e.

>var
>  st: string
>begin
>  st := 'Form1.Caption';

>I would like to use a list of variables like this to assign values from
>another list. I tried typecasting

>with (st as TObject) do
>  st := 'New Caption';

>But I'm missing something basic here. Can anyone help me out? Cheers.

Now, why on *earth* would you want to do this? Why do you need to store
the name of the property "Caption" when you know there is a property of
that name?

And you absolutely cannot typecast a string to a TObject!

You *can* have a string that contains the name of a component, and
use FindComponent with that string, but given the above example this
also seems a little pointless. Or, you could write a string parsing
routine that breaks up the string looking for a component name and
a property name, and then assigns something to that property, but
your parser would have to be very large and unwieldy since you'd have
to code in every possible property of every Delphi object. This is
equally futile.

Why not give some more information about what you're trying to do?

--
Jeremy Collins
Kansai Business Systems

(return address not altered 'coz I get spammed *whatever* I do!)

Re:Referencing objects with variables


Hi Jeremy

Quote
> Now, why on *earth* would you want to do this? Why do you need to store
> the name of the property "Caption" when you know there is a property of
> that name?

Thanks for your reply. I was trying to store a list of components and their
properties and then affect them in one of two ways when the form loads, i.e.
set all button captions to be prefixed with "t" if a certain condition was
true or set the prefix to "a" if the condition was false.

Because of the typecasting issues invloved with the huge number of components
that are available I didn't want to test each component for every type to
decide whether to set the Caption or the Text property. It then gets more
complex when setting the Column Titles in grids. Hence the desire to treat a
component and its properties as a component, that I could simply assign a
value to.

Quote
> You *can* have a string that contains the name of a component, and
> use FindComponent with that string, but given the above example this
> also seems a little pointless. Or, you could write a string parsing
> routine that breaks up the string looking for a component name and
> a property name, and then assigns something to that property, but
> your parser would have to be very large and unwieldy since you'd have
> to code in every possible property of every Delphi object. This is
> equally futile.

FindComponent, great, I missed that one in the help. Yes I could use that to
maybe do it in a different way. Many thanks Jeremy, I appreciate it.

I presume then from your reply that there is no logic in trying to treat a
component and a property as an object, bummer, back to the drawing board.

Cheers.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

Re:Referencing objects with variables


Quote
>Thanks for your reply. I was trying to store a list of components and their
>properties and then affect them in one of two ways when the form loads, i.e.
>set all button captions to be prefixed with "t" if a certain condition was
>true or set the prefix to "a" if the condition was false.

The Delphi VCL already provides you with a neat list of components.
Everything (including TForm) that is descended from TComponent has
a Components array, and a ComponentCount property. For example, to
change the captions of all labels on a form, you can do something like
this:

var
  i : integer;
begin
  for i := 0 to ComponentCount - 1 do
    if Components[i] is TLabel then
      TLabel(Components[i]).Caption := IntToStr(i);
end;

Note that you have to test and typecast Components[i] since it
references a TComponent.

Quote
>Because of the typecasting issues invloved with the huge number of components
>that are available I didn't want to test each component for every type to
>decide whether to set the Caption or the Text property. It then gets more
>complex when setting the Column Titles in grids. Hence the desire to treat a
>component and its properties as a component, that I could simply assign a
>value to.

OK, but there aren't that many different components on the majority of
forms that have a "visible text" property (it'll mostly be TButton,
TEdit, TLabel, TCheckBox and TRadioButton). This beats testing all
possible *properties* !

This is a little "advanced manual" tip, but you can expose protected
class properties simply by adding a line like this to the "type"
section of your source file:

type
  TExposedControl = class(TControl);

We can now do this:

procedure TForm1.Button1Click(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to ComponentCount - 1 do
    if Components[i] is TControl then begin
      TExposedControl(Components[i]).Caption := IntToStr(i);
      TExposedControl(Components[i]).Text := IntToStr(i);
    end;
end;

Try this with a test app (dump a load of different controls on a form).
This will set the Text and Caption property of *anything* on the form
without complaint.

Caveat - be *very* careful what you do with TExposedControl!

Quote
>I presume then from your reply that there is no logic in trying to treat a
>component and a property as an object

Oooh, nononono! <g>
--
Jeremy Collins
Kansai Business Systems

(return address not altered 'coz I get spammed *whatever* I do!)

Re:Referencing objects with variables


Hi Jeremy

Quote
> var
>   i : integer;
> begin
>   for i := 0 to ComponentCount - 1 do
>     if Components[i] is TLabel then
>       TLabel(Components[i]).Caption := IntToStr(i);
> end;

> Note that you have to test and typecast Components[i] since it
> references a TComponent.

After your earlier pointer I'd tried this route with some success, cheers.

Quote
> OK, but there aren't that many different components on the majority of
> forms that have a "visible text" property (it'll mostly be TButton,
> TEdit, TLabel, TCheckBox and TRadioButton). This beats testing all
> possible *properties* !

That's a good point, I'll do some research and see what it covers, now that
you mention it, there may be few exceptions.

Quote
> This is a little "advanced manual" tip, but you can expose protected
> class properties simply by adding a line like this to the "type"
> section of your source file:

> type
>   TExposedControl = class(TControl);

> We can now do this:

> procedure TForm1.Button1Click(Sender: TObject);
> var
>   i : integer;
> begin
>   for i := 0 to ComponentCount - 1 do
>     if Components[i] is TControl then begin
>       TExposedControl(Components[i]).Caption := IntToStr(i);
>       TExposedControl(Components[i]).Text := IntToStr(i);
>     end;
> end;

> Try this with a test app (dump a load of different controls on a form).
> This will set the Text and Caption property of *anything* on the form
> without complaint.

> Caveat - be *very* careful what you do with TExposedControl!

Oh, now that is really lovely! 'twas destiny that we met in {*word*104}space. Thanks
a million Jeremy! That's really helped,

Cheers.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

Re:Referencing objects with variables


Hi

To be able to do this with any object you need to access the Run Time Type
Information (RTTI), there are several
places on the web with descriptions of this including an article about
Understanding RTTI by Jani Jarvinen. I do not Have the URL's to hand but Jani's
even had a demo where you could enter lines like AddBtn1.Width = 100 and the
form would modify itself.

John

Quote
vig...@my-dejanews.com wrote:
> Hi,

> A simple one I'm afraid. I have a string with the name of an object and its
> property, i.e.

> var
>   st: string
> begin
>   st := 'Form1.Caption';

> I would like to use a list of variables like this to assign values from
> another list. I tried typecasting

> with (st as TObject) do
>   st := 'New Caption';

> But I'm missing something basic here. Can anyone help me out? Cheers.

> -----== Posted via Deja News, The Leader in Internet Discussion ==-----
> http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

Other Threads