hey all, have a code "convention" question, and was wondering if it's done this way as a "Best Practices Approach", and/or what the advantage is for doing it this way. Here's the example code from Amit's 70-315 book: private void dudColor_SelectedItemChanged(object sender, System.EventArgs e) { //Typecast the object to DomainUpDown DomainUpDown dudColor = (DomainUpDown) sender; //Change color of lblsampleText to selected color lblSampleText.ForeColor = Color.FromName (dudColor.Text); } private void nudSize_ValueChanged(object sender, System.EventArgs e) { //Typecast the object to NumericUpDown NumericUpDown nudSize = (NumericUpDown) sender; //Change the font of lblSampleText to selected font lblSampleText.Font = new Font (lblSampleText.Font.FontFamily, (float) nudSize.Value); } OK, I understand the code completely, but why is the sender cast to a new instance of the same object, when you can just directly access the 'sender' like this: private void dudColor_SelectedItemChanged(object sender, System.EventArgs e) { lblSampleText.ForeColor = Color.FromName (dudColor.Text); } private void nudSize_ValueChanged(object sender, System.EventArgs e) { lblSampleText.Font = new Font (lblSampleText.Font.FontFamily, (float) nudSize.Value); } The only advantage I can see to using the first approach, casting the sender is if you are attaching this event to multiple objects ... then you would be able to use this same set of code to make programmatic changes polymorphically (for any sender sent in). Thanks, Pete PS-sorry if formatting is off!
Pete, did you actually try your code and compiled it? dudColor and nudSize are both locally declared inside their respective events, in your code sample. I guess you just can't use sender.Text or sender.Value (inside the events) directly. You must have to declare a local DomainUpDown and NumericUpDown (or other object) and casting sender to the appropriate object. If you use the class declares dudColor and nudSize then you won't have the current Text or Value (that are stored in sender), coming from the event. Make sense? Jackie