 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Paul E. Schoen Guest
|
Posted: Mon Jan 16, 2006 8:17 am Post subject: Identifying Sender: TObject |
|
|
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
--
Paul E. Schoen, President
P S Technology, Inc.
Cockeysville, MD
www.pstech-inc.com
|
|
| Back to top |
|
 |
Nicholas Sherlock Guest
|
Posted: Mon Jan 16, 2006 8:43 am Post subject: Re: Identifying Sender: TObject |
|
|
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
|
Posted: Mon Jan 16, 2006 9:08 am Post subject: Re: Identifying Sender: TObject |
|
|
"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
|
Posted: Mon Jan 16, 2006 9:31 am Post subject: Re: Identifying Sender: TObject |
|
|
"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
|
Posted: Mon Jan 16, 2006 9:58 am Post subject: Re: Identifying Sender: TObject |
|
|
"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
|
Posted: Tue Jan 17, 2006 6:12 pm Post subject: Re: Identifying Sender: TObject |
|
|
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 |
|
 |
|
|
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
|
|