Help understanding delegates?

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

    Help understanding delegates?

    I'd like to better understand how the following code works. I've posted
    questions below.

    namespace Something.Somet hing1
    {
    using System;

    public delegate void Test1();
    public delegate void Test2(ink k);

    public class A
    {
    public static void Log1 (int l)
    {
    Console.WriteLi ne(":{0}",l);
    }

    public void Log2 (int l)
    {
    Console.WriteLi ne("#{0}",l);
    }
    }

    public class Class1
    {
    public static void Main()
    {
    A a = new A();
    Test2 t0 = new Test2(A.Log1); //use static method
    Test2 t1 = new Test2(a.Log2); //use instance method

    t0 += t1; t0(0);
    t0 -= t1; t0(1);
    }
    }
    }

    This will print ":0 #0 :1". How does Log1 print 0 when no param is passed
    to it and "l" is not initialized to 0?

    What are the differences in the static and instance method calls via
    delegates?

    This line t0 -= t1; t0(1); removes those methods from the delegate
    collection correct? How is the output affected if that line does not
    exists?



    Also, if I'm using the IE object and have three successive page loads, will
    each of the following events respectively fire after each page load rather
    than all three always firing after each page load? In other words, after
    the first page load completes, onIEDocComplete 1 fires. After the second
    page load completes, onIEDocComplete 2 fires. After the third page load
    completes, onIEDocComplete 3 fires.

    IE_Inst.Documen tComplete += new
    SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e1);

    IE_Inst.Documen tComplete += new
    SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e2);

    IE_Inst.Documen tComplete += new
    SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e3);

    Thanks,
    Brett


  • Mattias Sjögren

    #2
    Re: Help understanding delegates?

    [color=blue]
    >How does Log1 print 0 when no param is passed
    >to it and "l" is not initialized to 0?[/color]

    What do you mean no param is passed to it? You're passing 0 when
    invoking the delegate so that's the value l will get.

    [color=blue]
    >What are the differences in the static and instance method calls via
    >delegates?[/color]

    No differences apart from the ones seen in regular method calls.

    [color=blue]
    >This line t0 -= t1; t0(1); removes those methods from the delegate
    >collection correct?[/color]

    It removes the t1 methods (Log2) from t0, but leaves Log1.

    [color=blue]
    >How is the output affected if that line does not exists?[/color]

    Why don't you try it and see.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Brett

      #3
      Re: Help understanding delegates?


      "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
      news:ufOw9n$UFH A.4000@TK2MSFTN GP14.phx.gbl...[color=blue]
      >[color=green]
      >>How does Log1 print 0 when no param is passed
      >>to it and "l" is not initialized to 0?[/color]
      >
      > What do you mean no param is passed to it? You're passing 0 when
      > invoking the delegate so that's the value l will get.[/color]

      I mean to say t1 or Log2. Zero is not passed it yet it is displayed.

      Any suggestions on the IE question?


      Comment

      • Mattias Sjögren

        #4
        Re: Help understanding delegates?

        >>>How does Log1 print 0 when no param is passed[color=blue][color=green][color=darkred]
        >>>to it and "l" is not initialized to 0?[/color]
        >>
        >> What do you mean no param is passed to it? You're passing 0 when
        >> invoking the delegate so that's the value l will get.[/color]
        >
        >I mean to say t1 or Log2. Zero is not passed it yet it is displayed.[/color]

        Log2 is called with the argument 0 when you invoke the t0 delegate
        because Log2 is in t0's list of methods to call after you do t0 += t1.

        [color=blue]
        >Any suggestions on the IE question?[/color]
        [color=blue][color=green]
        >>Also, if I'm using the IE object and have three successive page loads, will
        >>each of the following events respectively fire after each page load rather
        >>than all three always firing after each page load?[/color][/color]

        No they will all fire for every load event. If that's not what you
        want, you'll have to remove the onIEDocComplete 1 handler and add the
        onIEDocComplete 2 one when onIEDocComplete 1 is called.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        • Brett

          #5
          Re: Help understanding delegates?


          "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
          news:OQGbKyAVFH A.3292@TK2MSFTN GP14.phx.gbl...[color=blue][color=green][color=darkred]
          >>>>How does Log1 print 0 when no param is passed
          >>>>to it and "l" is not initialized to 0?
          >>>
          >>> What do you mean no param is passed to it? You're passing 0 when
          >>> invoking the delegate so that's the value l will get.[/color]
          >>
          >>I mean to say t1 or Log2. Zero is not passed it yet it is displayed.[/color]
          >
          > Log2 is called with the argument 0 when you invoke the t0 delegate
          > because Log2 is in t0's list of methods to call after you do t0 += t1.
          >
          >[color=green]
          >>Any suggestions on the IE question?[/color]
          >[color=green][color=darkred]
          >>>Also, if I'm using the IE object and have three successive page loads,
          >>>will
          >>>each of the following events respectively fire after each page load
          >>>rather
          >>>than all three always firing after each page load?[/color][/color]
          >
          > No they will all fire for every load event. If that's not what you
          > want, you'll have to remove the onIEDocComplete 1 handler and add the
          > onIEDocComplete 2 one when onIEDocComplete 1 is called.
          >[/color]

          So is a delegate basically a collection of method pointers that when called,
          executes all methods in the collection in the order they were added to the
          collection?

          Brett


          Comment

          • Brett

            #6
            Re: Help understanding delegates?


            "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
            news:OQGbKyAVFH A.3292@TK2MSFTN GP14.phx.gbl...[color=blue][color=green][color=darkred]
            >>>>How does Log1 print 0 when no param is passed
            >>>>to it and "l" is not initialized to 0?
            >>>
            >>> What do you mean no param is passed to it? You're passing 0 when
            >>> invoking the delegate so that's the value l will get.[/color]
            >>
            >>I mean to say t1 or Log2. Zero is not passed it yet it is displayed.[/color]
            >
            > Log2 is called with the argument 0 when you invoke the t0 delegate
            > because Log2 is in t0's list of methods to call after you do t0 += t1.
            >
            >[color=green]
            >>Any suggestions on the IE question?[/color]
            >[color=green][color=darkred]
            >>>Also, if I'm using the IE object and have three successive page loads,
            >>>will
            >>>each of the following events respectively fire after each page load
            >>>rather
            >>>than all three always firing after each page load?[/color][/color]
            >
            > No they will all fire for every load event. If that's not what you
            > want, you'll have to remove the onIEDocComplete 1 handler and add the
            > onIEDocComplete 2 one when onIEDocComplete 1 is called.
            >
            >
            >
            > Mattias[/color]

            How exactly do I remove the IE delegate?

            I try:

            IE_Inst.Documen tComplete -=
            SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e);

            Compiler says:
            Method 'appname.IE.onI EDocComplete(ob ject, ref object)' referenced without
            parentheses

            So I try
            IE_Inst.Documen tComplete -=
            SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e());

            Compiler says:
            No overload for method 'onIEDocComplet e' takes '0' arguments

            Brett


            Comment

            • Mattias Sjögren

              #7
              Re: Help understanding delegates?

              [color=blue]
              >So is a delegate basically a collection of method pointers that when called,
              >executes all methods in the collection in the order they were added to the
              >collection?[/color]

              Correct, except that I believe the order they are called in is
              undefined and implementation dependent.

              [color=blue]
              >How exactly do I remove the IE delegate?
              >
              >I try:
              >
              >IE_Inst.Docume ntComplete -=
              >SHDocVw.DWebBr owserEvents2_Do cumentCompleteE ventHandler(thi s.onIEDocComple te);
              >
              >Compiler says:
              >Method 'appname.IE.onI EDocComplete(ob ject, ref object)' referenced without
              >parentheses[/color]

              Insert the new keyword before the event type.

              IE_Inst.Documen tComplete -= new
              SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e);



              Mattias

              --
              Mattias Sjögren [MVP] mattias @ mvps.org
              http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
              Please reply only to the newsgroup.

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Help understanding delegates?

                Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote:[color=blue][color=green]
                > >So is a delegate basically a collection of method pointers that when called,
                > >executes all methods in the collection in the order they were added to the
                > >collection?[/color]
                >
                > Correct, except that I believe the order they are called in is
                > undefined and implementation dependent.[/color]

                Nope - the order is guaranteed. Have a look at the docs for
                Delegate.Combin e for a few more details.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Help understanding delegates?

                  Jon Skeet [C# MVP] <skeet@pobox.co m> wrote:[color=blue][color=green]
                  > > Correct, except that I believe the order they are called in is
                  > > undefined and implementation dependent.[/color]
                  >
                  > Nope - the order is guaranteed. Have a look at the docs for
                  > Delegate.Combin e for a few more details.[/color]

                  Sorry, looking further, I can't see anything that absolutely guarantees
                  that when you call Invoke, the delegates are executed in the
                  appropriate order. However, things like the C# specification assume
                  that that's going to happen, and I think it's a pretty reasonable
                  assumption to make.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Willy Denoyette [MVP]

                    #10
                    Re: Help understanding delegates?


                    "Brett" <no@spam.net> wrote in message
                    news:%23MCoF6DV FHA.3696@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                    >
                    > "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                    > news:OQGbKyAVFH A.3292@TK2MSFTN GP14.phx.gbl...[color=green][color=darkred]
                    >>>>>How does Log1 print 0 when no param is passed
                    >>>>>to it and "l" is not initialized to 0?
                    >>>>
                    >>>> What do you mean no param is passed to it? You're passing 0 when
                    >>>> invoking the delegate so that's the value l will get.
                    >>>
                    >>>I mean to say t1 or Log2. Zero is not passed it yet it is displayed.[/color]
                    >>
                    >> Log2 is called with the argument 0 when you invoke the t0 delegate
                    >> because Log2 is in t0's list of methods to call after you do t0 += t1.
                    >>
                    >>[color=darkred]
                    >>>Any suggestions on the IE question?[/color]
                    >>[color=darkred]
                    >>>>Also, if I'm using the IE object and have three successive page loads,
                    >>>>will
                    >>>>each of the following events respectively fire after each page load
                    >>>>rather
                    >>>>than all three always firing after each page load?[/color]
                    >>
                    >> No they will all fire for every load event. If that's not what you
                    >> want, you'll have to remove the onIEDocComplete 1 handler and add the
                    >> onIEDocComplete 2 one when onIEDocComplete 1 is called.
                    >>
                    >>
                    >>
                    >> Mattias[/color]
                    >
                    > How exactly do I remove the IE delegate?
                    >
                    > I try:
                    >
                    > IE_Inst.Documen tComplete -=
                    > SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e);
                    >[/color]


                    You are missing the "new"

                    IE_Inst.Documen tComplete -= new ....

                    Not sure why you don't take some time to read the Events tutorial in MSDN's
                    "C# Programmers Reference"

                    Willy.




                    Comment

                    • Brett

                      #11
                      Re: Help understanding delegates?

                      -
                      "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
                      news:OOl8XxGVFH A.584@TK2MSFTNG P15.phx.gbl...[color=blue]
                      >
                      > "Brett" <no@spam.net> wrote in message
                      > news:%23MCoF6DV FHA.3696@TK2MSF TNGP10.phx.gbl. ..[color=green]
                      >>
                      >> "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                      >> news:OQGbKyAVFH A.3292@TK2MSFTN GP14.phx.gbl...[color=darkred]
                      >>>>>>How does Log1 print 0 when no param is passed
                      >>>>>>to it and "l" is not initialized to 0?
                      >>>>>
                      >>>>> What do you mean no param is passed to it? You're passing 0 when
                      >>>>> invoking the delegate so that's the value l will get.
                      >>>>
                      >>>>I mean to say t1 or Log2. Zero is not passed it yet it is displayed.
                      >>>
                      >>> Log2 is called with the argument 0 when you invoke the t0 delegate
                      >>> because Log2 is in t0's list of methods to call after you do t0 += t1.
                      >>>
                      >>>
                      >>>>Any suggestions on the IE question?
                      >>>
                      >>>>>Also, if I'm using the IE object and have three successive page loads,
                      >>>>>will
                      >>>>>each of the following events respectively fire after each page load
                      >>>>>rather
                      >>>>>than all three always firing after each page load?
                      >>>
                      >>> No they will all fire for every load event. If that's not what you
                      >>> want, you'll have to remove the onIEDocComplete 1 handler and add the
                      >>> onIEDocComplete 2 one when onIEDocComplete 1 is called.
                      >>>
                      >>>
                      >>>
                      >>> Mattias[/color]
                      >>
                      >> How exactly do I remove the IE delegate?
                      >>
                      >> I try:
                      >>
                      >> IE_Inst.Documen tComplete -=
                      >> SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e);
                      >>[/color]
                      >
                      >
                      > You are missing the "new"
                      >
                      > IE_Inst.Documen tComplete -= new ....
                      >
                      > Not sure why you don't take some time to read the Events tutorial in
                      > MSDN's "C# Programmers Reference"
                      >
                      > Willy.[/color]

                      Because it's not so clear how the particular thing with IE OnLoad is done.
                      You get much better feedback here anyway.

                      Brett


                      Comment

                      • Brett

                        #12
                        Re: Help understanding delegates?


                        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                        news:MPG.1ce911 1e4a99213498c0c b@msnews.micros oft.com...[color=blue]
                        > Jon Skeet [C# MVP] <skeet@pobox.co m> wrote:[color=green][color=darkred]
                        >> > Correct, except that I believe the order they are called in is
                        >> > undefined and implementation dependent.[/color]
                        >>
                        >> Nope - the order is guaranteed. Have a look at the docs for
                        >> Delegate.Combin e for a few more details.[/color]
                        >
                        > Sorry, looking further, I can't see anything that absolutely guarantees
                        > that when you call Invoke, the delegates are executed in the
                        > appropriate order. However, things like the C# specification assume
                        > that that's going to happen, and I think it's a pretty reasonable
                        > assumption to make.
                        >[/color]
                        I thought C# collections didn't have order?


                        Comment

                        • Willy Denoyette [MVP]

                          #13
                          Re: Help understanding delegates?


                          "Brett" <no@spam.net> wrote in message
                          news:OM5bwyHVFH A.2420@TK2MSFTN GP12.phx.gbl...[color=blue]
                          > -
                          > "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
                          > news:OOl8XxGVFH A.584@TK2MSFTNG P15.phx.gbl...[color=green]
                          >>
                          >> "Brett" <no@spam.net> wrote in message
                          >> news:%23MCoF6DV FHA.3696@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
                          >>>
                          >>> "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                          >>> news:OQGbKyAVFH A.3292@TK2MSFTN GP14.phx.gbl...
                          >>>>>>>How does Log1 print 0 when no param is passed
                          >>>>>>>to it and "l" is not initialized to 0?
                          >>>>>>
                          >>>>>> What do you mean no param is passed to it? You're passing 0 when
                          >>>>>> invoking the delegate so that's the value l will get.
                          >>>>>
                          >>>>>I mean to say t1 or Log2. Zero is not passed it yet it is displayed.
                          >>>>
                          >>>> Log2 is called with the argument 0 when you invoke the t0 delegate
                          >>>> because Log2 is in t0's list of methods to call after you do t0 += t1.
                          >>>>
                          >>>>
                          >>>>>Any suggestions on the IE question?
                          >>>>
                          >>>>>>Also, if I'm using the IE object and have three successive page loads,
                          >>>>>>will
                          >>>>>>each of the following events respectively fire after each page load
                          >>>>>>rather
                          >>>>>>than all three always firing after each page load?
                          >>>>
                          >>>> No they will all fire for every load event. If that's not what you
                          >>>> want, you'll have to remove the onIEDocComplete 1 handler and add the
                          >>>> onIEDocComplete 2 one when onIEDocComplete 1 is called.
                          >>>>
                          >>>>
                          >>>>
                          >>>> Mattias
                          >>>
                          >>> How exactly do I remove the IE delegate?
                          >>>
                          >>> I try:
                          >>>
                          >>> IE_Inst.Documen tComplete -=
                          >>> SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e);
                          >>>[/color]
                          >>
                          >>
                          >> You are missing the "new"
                          >>
                          >> IE_Inst.Documen tComplete -= new ....
                          >>
                          >> Not sure why you don't take some time to read the Events tutorial in
                          >> MSDN's "C# Programmers Reference"
                          >>
                          >> Willy.[/color]
                          >
                          > Because it's not so clear how the particular thing with IE OnLoad is done.
                          > You get much better feedback here anyway.
                          >
                          > Brett
                          >[/color]

                          If you did consult the documentation first, it wouldn't (maybe) be necessary
                          to ask questions like this one and you wouldn't ask about delegates while in
                          fact you were asking about Events.

                          Willy.



                          Comment

                          • Brett

                            #14
                            Re: Help understanding delegates?


                            "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
                            news:eMLevCIVFH A.2664@TK2MSFTN GP15.phx.gbl...[color=blue]
                            >
                            > "Brett" <no@spam.net> wrote in message
                            > news:OM5bwyHVFH A.2420@TK2MSFTN GP12.phx.gbl...[color=green]
                            >> -
                            >> "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
                            >> news:OOl8XxGVFH A.584@TK2MSFTNG P15.phx.gbl...[color=darkred]
                            >>>
                            >>> "Brett" <no@spam.net> wrote in message
                            >>> news:%23MCoF6DV FHA.3696@TK2MSF TNGP10.phx.gbl. ..
                            >>>>
                            >>>> "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                            >>>> news:OQGbKyAVFH A.3292@TK2MSFTN GP14.phx.gbl...
                            >>>>>>>>How does Log1 print 0 when no param is passed
                            >>>>>>>>to it and "l" is not initialized to 0?
                            >>>>>>>
                            >>>>>>> What do you mean no param is passed to it? You're passing 0 when
                            >>>>>>> invoking the delegate so that's the value l will get.
                            >>>>>>
                            >>>>>>I mean to say t1 or Log2. Zero is not passed it yet it is displayed.
                            >>>>>
                            >>>>> Log2 is called with the argument 0 when you invoke the t0 delegate
                            >>>>> because Log2 is in t0's list of methods to call after you do t0 += t1.
                            >>>>>
                            >>>>>
                            >>>>>>Any suggestions on the IE question?
                            >>>>>
                            >>>>>>>Also, if I'm using the IE object and have three successive page
                            >>>>>>>loads, will
                            >>>>>>>each of the following events respectively fire after each page load
                            >>>>>>>rather
                            >>>>>>>than all three always firing after each page load?
                            >>>>>
                            >>>>> No they will all fire for every load event. If that's not what you
                            >>>>> want, you'll have to remove the onIEDocComplete 1 handler and add the
                            >>>>> onIEDocComplete 2 one when onIEDocComplete 1 is called.
                            >>>>>
                            >>>>>
                            >>>>>
                            >>>>> Mattias
                            >>>>
                            >>>> How exactly do I remove the IE delegate?
                            >>>>
                            >>>> I try:
                            >>>>
                            >>>> IE_Inst.Documen tComplete -=
                            >>>> SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e);
                            >>>>
                            >>>
                            >>>
                            >>> You are missing the "new"
                            >>>
                            >>> IE_Inst.Documen tComplete -= new ....
                            >>>
                            >>> Not sure why you don't take some time to read the Events tutorial in
                            >>> MSDN's "C# Programmers Reference"
                            >>>
                            >>> Willy.[/color]
                            >>
                            >> Because it's not so clear how the particular thing with IE OnLoad is
                            >> done. You get much better feedback here anyway.
                            >>
                            >> Brett
                            >>[/color]
                            >
                            > If you did consult the documentation first, it wouldn't (maybe) be
                            > necessary to ask questions like this one and you wouldn't ask about
                            > delegates while in fact you were asking about Events.
                            >
                            > Willy.[/color]

                            Delegates and events are related. It's not uncommon that asking about one
                            brings up a question on the other...if you are a beginner.

                            Brett


                            Comment

                            • Jon Skeet [C# MVP]

                              #15
                              Re: Help understanding delegates?

                              Brett <no@spam.net> wrote:[color=blue][color=green][color=darkred]
                              > >> Nope - the order is guaranteed. Have a look at the docs for
                              > >> Delegate.Combin e for a few more details.[/color]
                              > >
                              > > Sorry, looking further, I can't see anything that absolutely guarantees
                              > > that when you call Invoke, the delegates are executed in the
                              > > appropriate order. However, things like the C# specification assume
                              > > that that's going to happen, and I think it's a pretty reasonable
                              > > assumption to make.
                              > >[/color]
                              > I thought C# collections didn't have order?[/color]

                              That entirely depends on the collection - and delegates aren't
                              collections in the normal sense anyway.

                              --
                              Jon Skeet - <skeet@pobox.co m>
                              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                              If replying to the group, please do not mail me too

                              Comment

                              Working...