Code Question 70-316 Amit Kalani Book

Discussion in 'MCAD' started by psinyc, Apr 4, 2004.

  1. psinyc

    psinyc Guest

    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!
     
    psinyc, Apr 4, 2004
    #1
    1. Advertising

  2. psinyc

    Jackie Guest

    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
    >-----Original Message-----
    >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!
    >.
    >
     
    Jackie, Apr 4, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard I.P
    Replies:
    0
    Views:
    481
    Richard I.P
    Jan 5, 2004
  2. Neel
    Replies:
    2
    Views:
    625
  3. Hari Krishna

    Amit Kalani 70-320 Book

    Hari Krishna, Nov 7, 2004, in forum: MCSD
    Replies:
    4
    Views:
    889
    The Poster Formerly Known as Kline Sphere
    Nov 8, 2004
  4. cSharpzone
    Replies:
    4
    Views:
    382
  5. RAB
    Replies:
    1
    Views:
    353
Loading...

Share This Page