BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Identifying Sender: TObject

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
Paul E. Schoen
Guest





PostPosted: Mon Jan 16, 2006 8:17 am    Post subject: Identifying Sender: TObject Reply with quote



In the following code, I can set a breakpoint as noted, and it is triggered,
indicating Sender = ReclData, as I expected. However, if I inspect Sender,
or WhoSentIt, the result is (); How could I identify the Sender if I didn't
already know all possible senders? How does the equality know?

var WhoSentIt: TObject;
procedure TReclData.FormClose(Sender: TObject; var Action: TCloseAction);
begin
If Sender = ReclData then
WhoSentIt := Sender; // Set Breakpoint Here
Action := caFree;
end;

This is just a matter of curiosity for now. I appreciate all help provided
on other more critical matters, and I'm sure I will have more as I try to
clean up several thousand lines of code Smile
--
Paul E. Schoen, President
P S Technology, Inc.
Cockeysville, MD
www.pstech-inc.com


Back to top
Nicholas Sherlock
Guest





PostPosted: Mon Jan 16, 2006 8:43 am    Post subject: Re: Identifying Sender: TObject Reply with quote



Paul E. Schoen wrote:
Quote:
If I inspect Sender, or WhoSentIt, the result is ();

This is simply because the IDE has no useful information to show you.

Quote:
How could I identify the Sender if I didn't
already know all possible senders? How does the equality know?

It checks to see if both pointers point to the same object.

Your question is odd, what exactly are you trying to achieve?

Cheers,
Nicholas Sherlock

Back to top
Tom de Neef
Guest





PostPosted: Mon Jan 16, 2006 9:08 am    Post subject: Re: Identifying Sender: TObject Reply with quote



"Paul E. Schoen" <pstech (AT) smart (DOT) net> schreef in bericht
news:11smlh4mdvv8741 (AT) corp (DOT) supernews.com...
Quote:
In the following code, I can set a breakpoint as noted, and it is
triggered,
indicating Sender = ReclData, as I expected. However, if I inspect Sender,
or WhoSentIt, the result is (); How could I identify the Sender if I
didn't
already know all possible senders? How does the equality know?

var WhoSentIt: TObject;
procedure TReclData.FormClose(Sender: TObject; var Action: TCloseAction);
begin
If Sender = ReclData then
WhoSentIt := Sender; // Set Breakpoint Here
Action := caFree;
end;


You can try evaluating Sender.classname or sender.classtype.
Once you know the class (eg Tedit), you can see more by typecasting sender,
eg Tedit(sender).name.
Tom



Back to top
Maarten Wiltink
Guest





PostPosted: Mon Jan 16, 2006 9:31 am    Post subject: Re: Identifying Sender: TObject Reply with quote

"Paul E. Schoen" <pstech (AT) smart (DOT) net> wrote


Quote:
In the following code, I can set a breakpoint as noted, and it is
triggered, indicating Sender = ReclData, as I expected. However,
if I inspect Sender, or WhoSentIt, the result is (); How could I
identify the Sender if I didn't already know all possible senders?
How does the equality know?

The equality is a simple pointer comparison. Sender and WhoSentIt
are of compatible types, so they can be compared. If Sender were a
TGraphicControl and WhoSentIt a TWinControl, you'd know in advance
they'd never be the same (unless both nil).

Note carefully what the debugger knows. Sender and WhoSentIt are
both TObjects. They _may_ be instances of derived classes, but not
necessarily. All the debugger knows for sure is TObject. TObject has
no fields, which is what it shows. Add a watch for TComponent(Sender)
and you'll see fields. Assuming Sender really is a TComponent. (The
watch window does not support "as" casting.)

As to _identifying_ the Sender, it's often not necessary. You should
be interested in its behaviour, not its identity. Say for the sake of
example that you have an event handler that disables the control that
fired the event. The Sender is declared TObject; it must be, there are
actually good reasons for that. You _check_ that Sender is a TControl,
cast it, and set its Enabled to False. Enabled is introduced in
TControl, so you need TControl, but you _don't_ need to know exactly
_which_ editbox or button you're talking to. Polymorphism in action.

There are cases where you really want to know which of a few possible
senders called an event handler, but they are IME rare.

Groetjes,
Maarten Wiltink



Back to top
Paul E. Schoen
Guest





PostPosted: Mon Jan 16, 2006 9:58 am    Post subject: Re: Identifying Sender: TObject Reply with quote


"Tom de Neef" <tdeneef (AT) qolor (DOT) nl> wrote

Quote:
"Paul E. Schoen" <pstech (AT) smart (DOT) net> schreef in bericht
news:11smlh4mdvv8741 (AT) corp (DOT) supernews.com...
In the following code, I can set a breakpoint as noted, and it is
triggered,
indicating Sender = ReclData, as I expected. However, if I inspect
Sender,
or WhoSentIt, the result is (); How could I identify the Sender if I
didn't
already know all possible senders? How does the equality know?

var WhoSentIt: TObject;
procedure TReclData.FormClose(Sender: TObject; var Action:
TCloseAction);
begin
If Sender = ReclData then
WhoSentIt := Sender; // Set Breakpoint Here
Action := caFree;
end;


You can try evaluating Sender.classname or sender.classtype.
Once you know the class (eg Tedit), you can see more by typecasting
sender,
eg Tedit(sender).name.
Tom


I used TReclData as a typecast, and it works perfectly. When I clicked a

button, it gave me the name of the button in its OnClick handler. When it
came from the OnClose event handler above, it gave me the form 'ReclData'.
It may come in handy at some time, probably for debugging. Thank You! :)

Paul



Back to top
Sonnich
Guest





PostPosted: Tue Jan 17, 2006 6:12 pm    Post subject: Re: Identifying Sender: TObject Reply with quote

Not that it matters, but this is somehow useful, e.g. when using the
OnMouseMove.
To give you some ideas, this is what I have been diong during times:

if Sender is TButton then...
if TButton(Sender) = btnClickMe then ....
if sender = nil then...

and things like that.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.