Board index » delphi » Newbie Brainspark (Sender as...)

Newbie Brainspark (Sender as...)

Ever since learning Delphi I was wondering.

Why do we need to say (Sender as TLabel), (Sender as TDBEdit) and so
on when referring to parameters that were sent as (Sender as TObject).

Sent as TObject, why can't we access the properties as
Sender.caption:=..."?

Instead, the compiler wants (Sender as TLabel).caption:=..." and says
"field identifier expected".

Is there any conceptual reason why Delphi might not understand this at
run time?

In other words, why can't we send a TBitBtn as a TObject and expect
Delphi to understand that the TObject is in fact a TBitBtn when it
arrives as a parameter - simply by virtue of the fact that it is what
it is.

Opinion: Conceptually, it should be possible for Delphi to know
exactly what type of TObject it is passing down at run time even if it
is not specified in the code. It seems Borland just haven't done it.

Perhaps their reason is that they think it helps prevent programmers
from accessing non-existing properties in their code.

Any ideas?

Email appreciated. I will reply.
Matthew

 

Re:Newbie Brainspark (Sender as...)


Quote
Matthew wrote:

> Ever since learning Delphi I was wondering.

> Why do we need to say (Sender as TLabel), (Sender as TDBEdit) and so
> on when referring to parameters that were sent as (Sender as TObject).

> Sent as TObject, why can't we access the properties as
> Sender.caption:=..."?

> Instead, the compiler wants (Sender as TLabel).caption:=..." and says
> "field identifier expected".

> Is there any conceptual reason why Delphi might not understand this at
> run time?

> In other words, why can't we send a TBitBtn as a TObject and expect
> Delphi to understand that the TObject is in fact a TBitBtn when it
> arrives as a parameter - simply by virtue of the fact that it is what
> it is.

> Opinion: Conceptually, it should be possible for Delphi to know
> exactly what type of TObject it is passing down at run time even if it
> is not specified in the code. It seems Borland just haven't done it.

> Perhaps their reason is that they think it helps prevent programmers
> from accessing non-existing properties in their code.

> Any ideas?

> Email appreciated. I will reply.
> Matthew

I think if you look at your inheritance trees inside of the browser you
will see that TObject is just the lowest common denominator that
everthing springs from. If all you want from the sender is information
in the senders tag then you don't need to know what type the sender is
just use

        (Sender as TComponent).Tag;

Yes it is more work for the programmer, but you shouldn't expect the
language and compiler to do all of your thinking for you.  Also the
extra work involved in finding the exact type of the sender each time
would add a lot of overhead to your programs, something most of us could
do without.

Keep up the good thoughts.

Dave Joyce

Re:Newbie Brainspark (Sender as...)


Quote
Matthew wrote:

> In other words, why can't we send a TBitBtn as a TObject and expect
> Delphi to understand that the TObject is in fact a TBitBtn when it
> arrives as a parameter - simply by virtue of the fact that it is what
> it is.

> Opinion: Conceptually, it should be possible for Delphi to know
> exactly what type of TObject it is passing down at run time even if it
> is not specified in the code. It seems Borland just haven't done it.

Run-time, no problem. The problem is actually at compile time.
When code is translated, the symbolic names you and I use are
translated into offsets from some base. To the compiler, a
class is just a record. Each attribute has it's own offset.
The variable 'Self' is just a pointer to this record (and hence
the 'base' mentioned above). In addition to the attributes you see,
there are a couple of 'hidden' ones, among others a pointer to a
table where all pointers to virtual functions are stored. This
pointer identifies the class of the object.) So, when you use
TLabel.Caption, the compiler figures out this is at address
[Self + 16] (for instance). However, if you use TForm.Caption,
the address might be [Self + 32] (for instance). So you'll have
to tell it what object to calculate reference for.

This is the prize of fast compiled code. It's fast, but you
have to tell it what you want, so it's not as dynamic. If you
want, you can use the RTTI functions to find published
properties by name at run-time. This is (not) described in the
TypInf.PAS file (or TypInf.INT if you're on Delphi 1).

If you get the chance, I highly recomend taking a compiler
construction class or reading a compiler construction book. It's
hard work, but you'll understand much more about the magic that
goes on when you hit F9.

M.

--
Martin Larsson, author of several unknown utilities for DOS and Windows.
mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no

Other Threads