XXXX_Load Event vs OnXXXX

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

    XXXX_Load Event vs OnXXXX

    I may have asked this before, but what is the purpose of both these
    functions? Is there a time when one should be called over the other?

    Does one supersede the other, or do they both have their time and place?

    TIA

    Charles


  • Bill McCarthy

    #2
    Re: XXXX_Load Event vs OnXXXX

    Hi Charles,

    I think you are referring to the XXXX Event versus the OnXXXX method, eg the
    Load Event versus the OnLoad method.

    To understand this pattern, it's probably easiest to look at a simple case.
    Let's assume you create a Class named A and a class named B, where B derives
    from A (that is:

    Class A
    ...
    End Class

    Class B : Inherits A
    ..
    End Class

    Now to A, add an Event Foo. Once you do this, in B you can actually subscribe
    to the event, but you cannot raise the event or block the event from raising.
    This is because only code in class A can raise the event. The reason for that is
    the event is actually a private multicast delegate and hence cannot be invoked
    from outside of the class, due to scoping rules. So to allow your derived class
    B to raise the event Foo as declared in A, the usual practice is to declare an
    Overridable method in A called OnFoo. The code in that method actually raises
    the event Foo. So from A, anywhere you would want to raise the event, you call
    the OnFoo method. This also allows B to override the OnFoo method, and take
    some action before and or after the event is raised, or even to stop the event
    being raised. And this is the important thing here, that code in B's OnFoo
    method that overrides it's base class A' OnFoo method, needs to pass the call to
    it's base class for the event to be fired. You do this via the MyBase keyword,
    eg MyBase.OnFoo. If you did not do this, then say another class C derived from
    B well it would not have the Foo event raised and could not raise it, as the B's
    OnFoo would be effectively blocking that event from being raised.

    As to which to use, in a derived class, that being overriding the base class's
    method, or using the event, well the event possibly has a slightly higher
    overhead, but probably not a significant factor (IMO). The override of the
    OnXXX method does have greater functionality, but it also means that you must be
    careful to ensure you call the base method.

    HTH's

    Bill.








    "Charles Law" <blank@nowhere. com> wrote in message
    news:e51EQo7FEH A.628@TK2MSFTNG P10.phx.gbl...[color=blue]
    > I may have asked this before, but what is the purpose of both these
    > functions? Is there a time when one should be called over the other?
    >
    > Does one supersede the other, or do they both have their time and place?
    >
    > TIA
    >
    > Charles
    >
    >[/color]


    Comment

    • Charles Law

      #3
      Re: XXXX_Load Event vs OnXXXX

      Hi Bill

      It certainly does help. Thanks.

      In the case of a Windows form, for example, that inherits from
      System.Windows. Forms.Form, there is a Form1_Load event and an OnLoad
      overridable function. If all I need to do is run some code when the form
      loads, should I use one of these in preference to the other, or does it not
      really matter in this case?

      Charles


      "Bill McCarthy <no spam>" <bill_mcc &#64; iprimus.com.au> wrote in message
      news:OBtt4m9FEH A.2576@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Hi Charles,
      >
      > I think you are referring to the XXXX Event versus the OnXXXX method, eg[/color]
      the[color=blue]
      > Load Event versus the OnLoad method.
      >
      > To understand this pattern, it's probably easiest to look at a simple[/color]
      case.[color=blue]
      > Let's assume you create a Class named A and a class named B, where B[/color]
      derives[color=blue]
      > from A (that is:
      >
      > Class A
      > ...
      > End Class
      >
      > Class B : Inherits A
      > ..
      > End Class
      >
      > Now to A, add an Event Foo. Once you do this, in B you can actually[/color]
      subscribe[color=blue]
      > to the event, but you cannot raise the event or block the event from[/color]
      raising.[color=blue]
      > This is because only code in class A can raise the event. The reason for[/color]
      that is[color=blue]
      > the event is actually a private multicast delegate and hence cannot be[/color]
      invoked[color=blue]
      > from outside of the class, due to scoping rules. So to allow your derived[/color]
      class[color=blue]
      > B to raise the event Foo as declared in A, the usual practice is to[/color]
      declare an[color=blue]
      > Overridable method in A called OnFoo. The code in that method actually[/color]
      raises[color=blue]
      > the event Foo. So from A, anywhere you would want to raise the event, you[/color]
      call[color=blue]
      > the OnFoo method. This also allows B to override the OnFoo method, and[/color]
      take[color=blue]
      > some action before and or after the event is raised, or even to stop the[/color]
      event[color=blue]
      > being raised. And this is the important thing here, that code in B's[/color]
      OnFoo[color=blue]
      > method that overrides it's base class A' OnFoo method, needs to pass the[/color]
      call to[color=blue]
      > it's base class for the event to be fired. You do this via the MyBase[/color]
      keyword,[color=blue]
      > eg MyBase.OnFoo. If you did not do this, then say another class C derived[/color]
      from[color=blue]
      > B well it would not have the Foo event raised and could not raise it, as[/color]
      the B's[color=blue]
      > OnFoo would be effectively blocking that event from being raised.
      >
      > As to which to use, in a derived class, that being overriding the base[/color]
      class's[color=blue]
      > method, or using the event, well the event possibly has a slightly higher
      > overhead, but probably not a significant factor (IMO). The override of[/color]
      the[color=blue]
      > OnXXX method does have greater functionality, but it also means that you[/color]
      must be[color=blue]
      > careful to ensure you call the base method.
      >
      > HTH's
      >
      > Bill.
      >
      >
      >
      >
      >
      >
      >
      >
      > "Charles Law" <blank@nowhere. com> wrote in message
      > news:e51EQo7FEH A.628@TK2MSFTNG P10.phx.gbl...[color=green]
      > > I may have asked this before, but what is the purpose of both these
      > > functions? Is there a time when one should be called over the other?
      > >
      > > Does one supersede the other, or do they both have their time and place?
      > >
      > > TIA
      > >
      > > Charles
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Charlie Williams

        #4
        Re: XXXX_Load Event vs OnXXXX

        You should override the method of the base class instead of subscribing to an event.

        When your code runs, an overriden methods can be called directly, whereas event subscribers must be enumerated and the specified delegate must be called. Overriding the proper method will save you several IL instructions.

        Comment

        • Bill McCarthy

          #5
          Re: XXXX_Load Event vs OnXXXX

          Hi Charles,

          It can be six of one or half a dozen of the other in most cases. That is, if
          you are not expecting your form to be derived from, then using the event is
          simpler as it means you don't have to ensure the call to the base method gets
          called.
          Overriding the OnXXXX does require you to write more code, even if it is simply
          calling the base class OnXXX. But it also gives you that extra flexibility in
          knowing exactly when your code gets called either side of rasing the event. If
          you think your form may be derived from, this can be crucial. The reason being
          that although most probably, your form's XXX event subscribing code would get
          called before any subscribing code in a further derived class, there is no
          guarantee it will. So if you need to initialize object or fields before a
          derived class accesses them, then the OnXXX override will give you that ability
          with certainty, whereas events won't.

          So as you can probably see, the OnXXX pattern is more powerful, and is probably
          preferable, but that doesn't mean that the event subscription is necessarily a
          bad thing, as long as you are aware of the limitations and requirements of both
          ;)

          Bill.



          "Charles Law" <blank@nowhere. com> wrote in message
          news:%23tiFKv$F EHA.2768@tk2msf tngp13.phx.gbl. ..[color=blue]
          > Hi Bill
          >
          > It certainly does help. Thanks.
          >
          > In the case of a Windows form, for example, that inherits from
          > System.Windows. Forms.Form, there is a Form1_Load event and an OnLoad
          > overridable function. If all I need to do is run some code when the form
          > loads, should I use one of these in preference to the other, or does it not
          > really matter in this case?
          >
          > Charles
          >
          >
          > "Bill McCarthy <no spam>" <bill_mcc &#64; iprimus.com.au> wrote in message
          > news:OBtt4m9FEH A.2576@TK2MSFTN GP11.phx.gbl...[color=green]
          > > Hi Charles,
          > >
          > > I think you are referring to the XXXX Event versus the OnXXXX method, eg[/color]
          > the[color=green]
          > > Load Event versus the OnLoad method.
          > >
          > > To understand this pattern, it's probably easiest to look at a simple[/color]
          > case.[color=green]
          > > Let's assume you create a Class named A and a class named B, where B[/color]
          > derives[color=green]
          > > from A (that is:
          > >
          > > Class A
          > > ...
          > > End Class
          > >
          > > Class B : Inherits A
          > > ..
          > > End Class
          > >
          > > Now to A, add an Event Foo. Once you do this, in B you can actually[/color]
          > subscribe[color=green]
          > > to the event, but you cannot raise the event or block the event from[/color]
          > raising.[color=green]
          > > This is because only code in class A can raise the event. The reason for[/color]
          > that is[color=green]
          > > the event is actually a private multicast delegate and hence cannot be[/color]
          > invoked[color=green]
          > > from outside of the class, due to scoping rules. So to allow your derived[/color]
          > class[color=green]
          > > B to raise the event Foo as declared in A, the usual practice is to[/color]
          > declare an[color=green]
          > > Overridable method in A called OnFoo. The code in that method actually[/color]
          > raises[color=green]
          > > the event Foo. So from A, anywhere you would want to raise the event, you[/color]
          > call[color=green]
          > > the OnFoo method. This also allows B to override the OnFoo method, and[/color]
          > take[color=green]
          > > some action before and or after the event is raised, or even to stop the[/color]
          > event[color=green]
          > > being raised. And this is the important thing here, that code in B's[/color]
          > OnFoo[color=green]
          > > method that overrides it's base class A' OnFoo method, needs to pass the[/color]
          > call to[color=green]
          > > it's base class for the event to be fired. You do this via the MyBase[/color]
          > keyword,[color=green]
          > > eg MyBase.OnFoo. If you did not do this, then say another class C derived[/color]
          > from[color=green]
          > > B well it would not have the Foo event raised and could not raise it, as[/color]
          > the B's[color=green]
          > > OnFoo would be effectively blocking that event from being raised.
          > >
          > > As to which to use, in a derived class, that being overriding the base[/color]
          > class's[color=green]
          > > method, or using the event, well the event possibly has a slightly higher
          > > overhead, but probably not a significant factor (IMO). The override of[/color]
          > the[color=green]
          > > OnXXX method does have greater functionality, but it also means that you[/color]
          > must be[color=green]
          > > careful to ensure you call the base method.
          > >
          > > HTH's
          > >
          > > Bill.
          > >
          > >
          > >
          > >
          > >
          > >
          > >
          > >
          > > "Charles Law" <blank@nowhere. com> wrote in message
          > > news:e51EQo7FEH A.628@TK2MSFTNG P10.phx.gbl...[color=darkred]
          > > > I may have asked this before, but what is the purpose of both these
          > > > functions? Is there a time when one should be called over the other?
          > > >
          > > > Does one supersede the other, or do they both have their time and place?
          > > >
          > > > TIA
          > > >
          > > > Charles
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Bill McCarthy

            #6
            Re: XXXX_Load Event vs OnXXXX

            Hi Charles,

            It can be six of one or half a dozen of the other in most cases. That is, if
            you are not expecting your form to be derived from, then using the event is
            simpler as it means you don't have to ensure the call to the base method gets
            called.
            Overriding the OnXXXX does require you to write more code, even if it is simply
            calling the base class OnXXX. But it also gives you that extra flexibility in
            knowing exactly when your code gets called either side of rasing the event. If
            you think your form may be derived from, this can be crucial. The reason being
            that although most probably, your form's XXX event subscribing code would get
            called before any subscribing code in a further derived class, there is no
            guarantee it will. So if you need to initialize object or fields before a
            derived class accesses them, then the OnXXX override will give you that ability
            with certainty, whereas events won't.

            So as you can probably see, the OnXXX pattern is more powerful, and is probably
            preferable, but that doesn't mean that the event subscription is necessarily a
            bad thing, as long as you are aware of the limitations and requirements of both
            ;)

            Bill.



            "Charles Law" <blank@nowhere. com> wrote in message
            news:%23tiFKv$F EHA.2768@tk2msf tngp13.phx.gbl. ..[color=blue]
            > Hi Bill
            >
            > It certainly does help. Thanks.
            >
            > In the case of a Windows form, for example, that inherits from
            > System.Windows. Forms.Form, there is a Form1_Load event and an OnLoad
            > overridable function. If all I need to do is run some code when the form
            > loads, should I use one of these in preference to the other, or does it not
            > really matter in this case?
            >
            > Charles
            >
            >
            > "Bill McCarthy <no spam>" <bill_mcc &#64; iprimus.com.au> wrote in message
            > news:OBtt4m9FEH A.2576@TK2MSFTN GP11.phx.gbl...[color=green]
            > > Hi Charles,
            > >
            > > I think you are referring to the XXXX Event versus the OnXXXX method, eg[/color]
            > the[color=green]
            > > Load Event versus the OnLoad method.
            > >
            > > To understand this pattern, it's probably easiest to look at a simple[/color]
            > case.[color=green]
            > > Let's assume you create a Class named A and a class named B, where B[/color]
            > derives[color=green]
            > > from A (that is:
            > >
            > > Class A
            > > ...
            > > End Class
            > >
            > > Class B : Inherits A
            > > ..
            > > End Class
            > >
            > > Now to A, add an Event Foo. Once you do this, in B you can actually[/color]
            > subscribe[color=green]
            > > to the event, but you cannot raise the event or block the event from[/color]
            > raising.[color=green]
            > > This is because only code in class A can raise the event. The reason for[/color]
            > that is[color=green]
            > > the event is actually a private multicast delegate and hence cannot be[/color]
            > invoked[color=green]
            > > from outside of the class, due to scoping rules. So to allow your derived[/color]
            > class[color=green]
            > > B to raise the event Foo as declared in A, the usual practice is to[/color]
            > declare an[color=green]
            > > Overridable method in A called OnFoo. The code in that method actually[/color]
            > raises[color=green]
            > > the event Foo. So from A, anywhere you would want to raise the event, you[/color]
            > call[color=green]
            > > the OnFoo method. This also allows B to override the OnFoo method, and[/color]
            > take[color=green]
            > > some action before and or after the event is raised, or even to stop the[/color]
            > event[color=green]
            > > being raised. And this is the important thing here, that code in B's[/color]
            > OnFoo[color=green]
            > > method that overrides it's base class A' OnFoo method, needs to pass the[/color]
            > call to[color=green]
            > > it's base class for the event to be fired. You do this via the MyBase[/color]
            > keyword,[color=green]
            > > eg MyBase.OnFoo. If you did not do this, then say another class C derived[/color]
            > from[color=green]
            > > B well it would not have the Foo event raised and could not raise it, as[/color]
            > the B's[color=green]
            > > OnFoo would be effectively blocking that event from being raised.
            > >
            > > As to which to use, in a derived class, that being overriding the base[/color]
            > class's[color=green]
            > > method, or using the event, well the event possibly has a slightly higher
            > > overhead, but probably not a significant factor (IMO). The override of[/color]
            > the[color=green]
            > > OnXXX method does have greater functionality, but it also means that you[/color]
            > must be[color=green]
            > > careful to ensure you call the base method.
            > >
            > > HTH's
            > >
            > > Bill.
            > >
            > >
            > >
            > >
            > >
            > >
            > >
            > >
            > > "Charles Law" <blank@nowhere. com> wrote in message
            > > news:e51EQo7FEH A.628@TK2MSFTNG P10.phx.gbl...[color=darkred]
            > > > I may have asked this before, but what is the purpose of both these
            > > > functions? Is there a time when one should be called over the other?
            > > >
            > > > Does one supersede the other, or do they both have their time and place?
            > > >
            > > > TIA
            > > >
            > > > Charles
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Charles Law

              #7
              Re: XXXX_Load Event vs OnXXXX

              Thanks for the extra detail Bill.

              Cheers.

              Charles


              "Bill McCarthy <no spam>" <bill_mcc &#64; iprimus.com.au> wrote in message
              news:eTElYsFGEH A.3984@TK2MSFTN GP10.phx.gbl...[color=blue]
              > Hi Charles,
              >
              > It can be six of one or half a dozen of the other in most cases. That is,[/color]
              if[color=blue]
              > you are not expecting your form to be derived from, then using the event[/color]
              is[color=blue]
              > simpler as it means you don't have to ensure the call to the base method[/color]
              gets[color=blue]
              > called.
              > Overriding the OnXXXX does require you to write more code, even if it is[/color]
              simply[color=blue]
              > calling the base class OnXXX. But it also gives you that extra[/color]
              flexibility in[color=blue]
              > knowing exactly when your code gets called either side of rasing the[/color]
              event. If[color=blue]
              > you think your form may be derived from, this can be crucial. The reason[/color]
              being[color=blue]
              > that although most probably, your form's XXX event subscribing code would[/color]
              get[color=blue]
              > called before any subscribing code in a further derived class, there is no
              > guarantee it will. So if you need to initialize object or fields before a
              > derived class accesses them, then the OnXXX override will give you that[/color]
              ability[color=blue]
              > with certainty, whereas events won't.
              >
              > So as you can probably see, the OnXXX pattern is more powerful, and is[/color]
              probably[color=blue]
              > preferable, but that doesn't mean that the event subscription is[/color]
              necessarily a[color=blue]
              > bad thing, as long as you are aware of the limitations and requirements of[/color]
              both[color=blue]
              > ;)
              >
              > Bill.
              >
              >
              >
              > "Charles Law" <blank@nowhere. com> wrote in message
              > news:%23tiFKv$F EHA.2768@tk2msf tngp13.phx.gbl. ..[color=green]
              > > Hi Bill
              > >
              > > It certainly does help. Thanks.
              > >
              > > In the case of a Windows form, for example, that inherits from
              > > System.Windows. Forms.Form, there is a Form1_Load event and an OnLoad
              > > overridable function. If all I need to do is run some code when the form
              > > loads, should I use one of these in preference to the other, or does it[/color][/color]
              not[color=blue][color=green]
              > > really matter in this case?
              > >
              > > Charles
              > >
              > >
              > > "Bill McCarthy <no spam>" <bill_mcc &#64; iprimus.com.au> wrote in[/color][/color]
              message[color=blue][color=green]
              > > news:OBtt4m9FEH A.2576@TK2MSFTN GP11.phx.gbl...[color=darkred]
              > > > Hi Charles,
              > > >
              > > > I think you are referring to the XXXX Event versus the OnXXXX method,[/color][/color][/color]
              eg[color=blue][color=green]
              > > the[color=darkred]
              > > > Load Event versus the OnLoad method.
              > > >
              > > > To understand this pattern, it's probably easiest to look at a simple[/color]
              > > case.[color=darkred]
              > > > Let's assume you create a Class named A and a class named B, where B[/color]
              > > derives[color=darkred]
              > > > from A (that is:
              > > >
              > > > Class A
              > > > ...
              > > > End Class
              > > >
              > > > Class B : Inherits A
              > > > ..
              > > > End Class
              > > >
              > > > Now to A, add an Event Foo. Once you do this, in B you can actually[/color]
              > > subscribe[color=darkred]
              > > > to the event, but you cannot raise the event or block the event from[/color]
              > > raising.[color=darkred]
              > > > This is because only code in class A can raise the event. The reason[/color][/color][/color]
              for[color=blue][color=green]
              > > that is[color=darkred]
              > > > the event is actually a private multicast delegate and hence cannot be[/color]
              > > invoked[color=darkred]
              > > > from outside of the class, due to scoping rules. So to allow your[/color][/color][/color]
              derived[color=blue][color=green]
              > > class[color=darkred]
              > > > B to raise the event Foo as declared in A, the usual practice is to[/color]
              > > declare an[color=darkred]
              > > > Overridable method in A called OnFoo. The code in that method[/color][/color][/color]
              actually[color=blue][color=green]
              > > raises[color=darkred]
              > > > the event Foo. So from A, anywhere you would want to raise the event,[/color][/color][/color]
              you[color=blue][color=green]
              > > call[color=darkred]
              > > > the OnFoo method. This also allows B to override the OnFoo method,[/color][/color][/color]
              and[color=blue][color=green]
              > > take[color=darkred]
              > > > some action before and or after the event is raised, or even to stop[/color][/color][/color]
              the[color=blue][color=green]
              > > event[color=darkred]
              > > > being raised. And this is the important thing here, that code in B's[/color]
              > > OnFoo[color=darkred]
              > > > method that overrides it's base class A' OnFoo method, needs to pass[/color][/color][/color]
              the[color=blue][color=green]
              > > call to[color=darkred]
              > > > it's base class for the event to be fired. You do this via the MyBase[/color]
              > > keyword,[color=darkred]
              > > > eg MyBase.OnFoo. If you did not do this, then say another class C[/color][/color][/color]
              derived[color=blue][color=green]
              > > from[color=darkred]
              > > > B well it would not have the Foo event raised and could not raise it,[/color][/color][/color]
              as[color=blue][color=green]
              > > the B's[color=darkred]
              > > > OnFoo would be effectively blocking that event from being raised.
              > > >
              > > > As to which to use, in a derived class, that being overriding the base[/color]
              > > class's[color=darkred]
              > > > method, or using the event, well the event possibly has a slightly[/color][/color][/color]
              higher[color=blue][color=green][color=darkred]
              > > > overhead, but probably not a significant factor (IMO). The override[/color][/color][/color]
              of[color=blue][color=green]
              > > the[color=darkred]
              > > > OnXXX method does have greater functionality, but it also means that[/color][/color][/color]
              you[color=blue][color=green]
              > > must be[color=darkred]
              > > > careful to ensure you call the base method.
              > > >
              > > > HTH's
              > > >
              > > > Bill.
              > > >
              > > >
              > > >
              > > >
              > > >
              > > >
              > > >
              > > >
              > > > "Charles Law" <blank@nowhere. com> wrote in message
              > > > news:e51EQo7FEH A.628@TK2MSFTNG P10.phx.gbl...
              > > > > I may have asked this before, but what is the purpose of both these
              > > > > functions? Is there a time when one should be called over the other?
              > > > >
              > > > > Does one supersede the other, or do they both have their time and[/color][/color][/color]
              place?[color=blue][color=green][color=darkred]
              > > > >
              > > > > TIA
              > > > >
              > > > > Charles
              > > > >
              > > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Charles Law

                #8
                Re: XXXX_Load Event vs OnXXXX

                Thanks for the extra detail Bill.

                Cheers.

                Charles


                "Bill McCarthy <no spam>" <bill_mcc &#64; iprimus.com.au> wrote in message
                news:eTElYsFGEH A.3984@TK2MSFTN GP10.phx.gbl...[color=blue]
                > Hi Charles,
                >
                > It can be six of one or half a dozen of the other in most cases. That is,[/color]
                if[color=blue]
                > you are not expecting your form to be derived from, then using the event[/color]
                is[color=blue]
                > simpler as it means you don't have to ensure the call to the base method[/color]
                gets[color=blue]
                > called.
                > Overriding the OnXXXX does require you to write more code, even if it is[/color]
                simply[color=blue]
                > calling the base class OnXXX. But it also gives you that extra[/color]
                flexibility in[color=blue]
                > knowing exactly when your code gets called either side of rasing the[/color]
                event. If[color=blue]
                > you think your form may be derived from, this can be crucial. The reason[/color]
                being[color=blue]
                > that although most probably, your form's XXX event subscribing code would[/color]
                get[color=blue]
                > called before any subscribing code in a further derived class, there is no
                > guarantee it will. So if you need to initialize object or fields before a
                > derived class accesses them, then the OnXXX override will give you that[/color]
                ability[color=blue]
                > with certainty, whereas events won't.
                >
                > So as you can probably see, the OnXXX pattern is more powerful, and is[/color]
                probably[color=blue]
                > preferable, but that doesn't mean that the event subscription is[/color]
                necessarily a[color=blue]
                > bad thing, as long as you are aware of the limitations and requirements of[/color]
                both[color=blue]
                > ;)
                >
                > Bill.
                >
                >
                >
                > "Charles Law" <blank@nowhere. com> wrote in message
                > news:%23tiFKv$F EHA.2768@tk2msf tngp13.phx.gbl. ..[color=green]
                > > Hi Bill
                > >
                > > It certainly does help. Thanks.
                > >
                > > In the case of a Windows form, for example, that inherits from
                > > System.Windows. Forms.Form, there is a Form1_Load event and an OnLoad
                > > overridable function. If all I need to do is run some code when the form
                > > loads, should I use one of these in preference to the other, or does it[/color][/color]
                not[color=blue][color=green]
                > > really matter in this case?
                > >
                > > Charles
                > >
                > >
                > > "Bill McCarthy <no spam>" <bill_mcc &#64; iprimus.com.au> wrote in[/color][/color]
                message[color=blue][color=green]
                > > news:OBtt4m9FEH A.2576@TK2MSFTN GP11.phx.gbl...[color=darkred]
                > > > Hi Charles,
                > > >
                > > > I think you are referring to the XXXX Event versus the OnXXXX method,[/color][/color][/color]
                eg[color=blue][color=green]
                > > the[color=darkred]
                > > > Load Event versus the OnLoad method.
                > > >
                > > > To understand this pattern, it's probably easiest to look at a simple[/color]
                > > case.[color=darkred]
                > > > Let's assume you create a Class named A and a class named B, where B[/color]
                > > derives[color=darkred]
                > > > from A (that is:
                > > >
                > > > Class A
                > > > ...
                > > > End Class
                > > >
                > > > Class B : Inherits A
                > > > ..
                > > > End Class
                > > >
                > > > Now to A, add an Event Foo. Once you do this, in B you can actually[/color]
                > > subscribe[color=darkred]
                > > > to the event, but you cannot raise the event or block the event from[/color]
                > > raising.[color=darkred]
                > > > This is because only code in class A can raise the event. The reason[/color][/color][/color]
                for[color=blue][color=green]
                > > that is[color=darkred]
                > > > the event is actually a private multicast delegate and hence cannot be[/color]
                > > invoked[color=darkred]
                > > > from outside of the class, due to scoping rules. So to allow your[/color][/color][/color]
                derived[color=blue][color=green]
                > > class[color=darkred]
                > > > B to raise the event Foo as declared in A, the usual practice is to[/color]
                > > declare an[color=darkred]
                > > > Overridable method in A called OnFoo. The code in that method[/color][/color][/color]
                actually[color=blue][color=green]
                > > raises[color=darkred]
                > > > the event Foo. So from A, anywhere you would want to raise the event,[/color][/color][/color]
                you[color=blue][color=green]
                > > call[color=darkred]
                > > > the OnFoo method. This also allows B to override the OnFoo method,[/color][/color][/color]
                and[color=blue][color=green]
                > > take[color=darkred]
                > > > some action before and or after the event is raised, or even to stop[/color][/color][/color]
                the[color=blue][color=green]
                > > event[color=darkred]
                > > > being raised. And this is the important thing here, that code in B's[/color]
                > > OnFoo[color=darkred]
                > > > method that overrides it's base class A' OnFoo method, needs to pass[/color][/color][/color]
                the[color=blue][color=green]
                > > call to[color=darkred]
                > > > it's base class for the event to be fired. You do this via the MyBase[/color]
                > > keyword,[color=darkred]
                > > > eg MyBase.OnFoo. If you did not do this, then say another class C[/color][/color][/color]
                derived[color=blue][color=green]
                > > from[color=darkred]
                > > > B well it would not have the Foo event raised and could not raise it,[/color][/color][/color]
                as[color=blue][color=green]
                > > the B's[color=darkred]
                > > > OnFoo would be effectively blocking that event from being raised.
                > > >
                > > > As to which to use, in a derived class, that being overriding the base[/color]
                > > class's[color=darkred]
                > > > method, or using the event, well the event possibly has a slightly[/color][/color][/color]
                higher[color=blue][color=green][color=darkred]
                > > > overhead, but probably not a significant factor (IMO). The override[/color][/color][/color]
                of[color=blue][color=green]
                > > the[color=darkred]
                > > > OnXXX method does have greater functionality, but it also means that[/color][/color][/color]
                you[color=blue][color=green]
                > > must be[color=darkred]
                > > > careful to ensure you call the base method.
                > > >
                > > > HTH's
                > > >
                > > > Bill.
                > > >
                > > >
                > > >
                > > >
                > > >
                > > >
                > > >
                > > >
                > > > "Charles Law" <blank@nowhere. com> wrote in message
                > > > news:e51EQo7FEH A.628@TK2MSFTNG P10.phx.gbl...
                > > > > I may have asked this before, but what is the purpose of both these
                > > > > functions? Is there a time when one should be called over the other?
                > > > >
                > > > > Does one supersede the other, or do they both have their time and[/color][/color][/color]
                place?[color=blue][color=green][color=darkred]
                > > > >
                > > > > TIA
                > > > >
                > > > > Charles
                > > > >
                > > > >
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                Working...