object sender question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Blair

    object sender question

    Hi,



    dgTransaction_S electedIndexCha nged(object sender, System.EventArg s e)

    That sender is actually the object that raised the event.

    So, in this case you could do:

    ((DataGrid)send er).Items[i].Controls[0].ClientID; //Do something
    meaningful here!

    What is the difference (if any) between using the sender object cast to
    the correct object type over just using the variable name directly:

    dgTransaction.I tems[i].Controls[0].ClientID;

    Any help on this would be appreciated.

    Regards,

    Steven


    *** Sent via Developersdex http://www.developersdex.com ***
  • Kalpesh

    #2
    Re: object sender question

    Hi,

    IMO, it is better to use control name directly (instead of casting the
    sender)
    The visible difference is the cast operation.

    Also, If you dont cast, you will get compile time error

    HTH
    Kalpesh

    Comment

    • Christof Nordiek

      #3
      Re: object sender question

      "Steven Blair" <steven.blair@b tinternet.com> schrieb im Newsbeitrag
      news:e$Rw12ZxFH A.3436@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi,
      > dgTransaction_S electedIndexCha nged(object sender, System.EventArg s e)
      >
      > That sender is actually the object that raised the event.
      >
      > So, in this case you could do:
      >
      > ((DataGrid)send er).Items[i].Controls[0].ClientID; //Do something
      > meaningful here!
      >
      > What is the difference (if any) between using the sender object cast to
      > the correct object type over just using the variable name directly:
      >[/color]
      <snip>
      You can use one Eventhandler for several objects.
      Than you need the object parameter, to know wich object fired the event.

      Christof


      Comment

      • Steven Blair

        #4
        Re: object sender question

        How would I go about doing that?

        Are you saying I could have 2 DataGrids, but one OnClick method, then
        using the sender, I could determine which button fired the event:

        this.dgTran1.Se lectIndexChange d += new
        System.EventHan dler(this.My_Se lectedIndexChan ged);

        this.dgTran2.Se lectIndexChange d += new
        System.EventHan dler(this.My_Se lectedIndexChan ged);

        And my event code:

        private void My_SelectedInde xChanged(object sender, System.EventArg s e)
        {
        //Somehow work out which DataGrid fired the event from?
        }



        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • Joanna Carter [TeamB]

          #5
          Re: object sender question

          "Steven Blair" <steven.blair@b tinternet.com> a écrit dans le message de
          news: eOfVHucxFHA.358 8@tk2msftngp13. phx.gbl...

          | Are you saying I could have 2 DataGrids, but one OnClick method, then
          | using the sender, I could determine which button fired the event:

          Well, it always works in Delphi :-)

          However, unless you have a lot of code in common, then I would recommend
          that you use two separate handlers. even if there is code in common it is
          better practice to have two handlers and call a common routine passing the
          sender to that.

          Joanna

          --
          Joanna Carter [TeamB]
          Consultant Software Engineer


          Comment

          • SP

            #6
            Re: object sender question


            "Steven Blair" <steven.blair@b tinternet.com> wrote in message
            news:eOfVHucxFH A.3588@tk2msftn gp13.phx.gbl...[color=blue]
            > How would I go about doing that?[/color]

            By casting the sender to a DataGrid exactly as you did in your original
            question.

            SP
            [color=blue]
            >
            > Are you saying I could have 2 DataGrids, but one OnClick method, then
            > using the sender, I could determine which button fired the event:
            >
            > this.dgTran1.Se lectIndexChange d += new
            > System.EventHan dler(this.My_Se lectedIndexChan ged);
            >
            > this.dgTran2.Se lectIndexChange d += new
            > System.EventHan dler(this.My_Se lectedIndexChan ged);
            >
            > And my event code:
            >
            > private void My_SelectedInde xChanged(object sender, System.EventArg s e)
            > {
            > //Somehow work out which DataGrid fired the event from?[/color]

            the sender obviously!!!

            [color=blue]
            > }
            >
            >
            >
            > *** Sent via Developersdex http://www.developersdex.com ***[/color]


            Comment

            • Chris Dunaway

              #7
              Re: object sender question

              Steven Blair wrote:[color=blue]
              > How would I go about doing that?[/color]

              That what the sender argument is for so you can determine which object
              raised the event.

              You could do something like this (Watch for typos):

              if (sender is DataGrid1)
              //Do something here

              Comment

              • Joanna Carter [TeamB]

                #8
                Re: object sender question

                "Chris Dunaway" <dunawayc@gmail .com> a écrit dans le message de news:
                1128105829.2895 73.324040@g14g2 00...legr oups.com...

                | You could do something like this (Watch for typos):
                |
                | if (sender is DataGrid1)
                | //Do something here

                The "is" syntax would compare sender's type to the class DataGrid1, not to a
                component held in a field called DataGrid1.

                You should use :

                if (sender == DataGrid1)
                ...

                Joanna

                --
                Joanna Carter [TeamB]
                Consultant Software Engineer


                Comment

                • Steven Blair

                  #9
                  Re: object sender question

                  Thanks for the help.



                  *** Sent via Developersdex http://www.developersdex.com ***

                  Comment

                  Working...