Re: this cf sender in event handlers
- From: "Gary Stephenson" <gary@xxxxxxxxxxxx>
- Date: Mon, 11 Jun 2007 21:08:40 +1000
Hmm, I suppose I should have been more precise by pointing out that "sender
as MyButton" does not return null - hence sender is definitely an instance
of MyButton (as expected since the event is actually raised by clicking the
button in question) - but apparently not (always) the same instance as
"this".
I see that another respondent found the assertion not to fail in their
testing, so I remain at a loss. Perhaps the question should be rephrased
as: Under what circumstances might the assertion be made to fail?
thanks,
gary
--
http://www.oxide.net.au
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.20d6fcf1fc6e04971ff@xxxxxxxxxxxxxxxxxxxxxxx
Gary Stephenson <gary@xxxxxxxxxxxx> wrote:
Can someone please explain why the assertion in the following code fails?
Could the assertion be made to succeed somehow?
// code begins
class MyButton : Button
{
public MyButton()
{
Click += HandleClick ;
}
public void HandleClick( object sender, EventArgs e)
{
Debug.Assert( Object.ReferenceEquals( sender as MyButton, this )
;
// why does this fail?
}
}
Well, the first thing to do would be to look at what the sender
*actually* is.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- References:
- this cf sender in event handlers
- From: Gary Stephenson
- Re: this cf sender in event handlers
- From: Jon Skeet [C# MVP]
- this cf sender in event handlers
- Prev by Date: Re: Difference, == and Object.Equals
- Next by Date: How to GetDecimal from database by ColumnName (NOT: by column number) ?
- Previous by thread: Re: this cf sender in event handlers
- Next by thread: Re: this cf sender in event handlers
- Index(es):