Why does this code run twice

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

    #1

    Why does this code run twice

    Hello,

    I can find no reason for this code to run twice. Can you help me?

    It may have something to do with it being a overrides sub but stepping
    through during debug does *not* bear this out.


    In the base class form...
    \\
    Protected Overridable Sub btnInsertCmptPr ofile_Click(ByV al sender As
    System.Object, _
    ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click

    Throw New NotImplementedE xception

    End Sub
    //

    In the derived form...
    \\
    Protected Overrides Sub btnInsertCmptPr ofile_Click(ByV al sender As
    System.Object, _
    ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click

    'Get profiledata from master
    Call InsertCmptProfi le()

    'Creates two new row objects in code and saves them to the database
    Call InsertValveForC ylinder()

    'Retreive new and current data
    Call ClearAndReloadF orm()

    End Sub '<<(stepping through in debug mode) the code from here returns
    to the top of this Sub and starts over. Why?
    //

    While stepping through the code I discovered that it just runs the
    derived code block twice. Why? What is the correct why to make it run
    once?

    Thank you,
    dbuchanan

  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Why does this code run twice

    dbuchanan,
    | While stepping through the code I discovered that it just runs the
    | derived code block twice. Why?
    Because your base class is handling the event and also your derived class,

    | What is the correct why to make it run
    | once?
    Rather then have btnInsertCmptPr ofile_Click as Overridable I would simply
    have an overridable OnInsertCmptPro file that the event handler calls. This
    hides (encapsulates) that an event is involved, which allows
    OnInsertCmptPro file to possibly be called in other cases...

    | In the base class form...
    | \\
    | Private Overridable Sub btnInsertCmptPr ofile_Click(ByV al sender As
    | System.Object, _
    | ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click
    OnInsertCmptPro file()
    End Sub

    Protected Overridable Sub OnInsertCmptPro file ()
    | Throw New NotImplementedE xception
    | End Sub

    | In the derived form...
    | \\
    | Protected Overrides Sub OnInsertCmptPro file()
    ...
    | End Sub


    Remember that one or more event handlers can each handle one or more events.

    Private Sub AnEventHandler( sender As Object, e As EventArgs) _
    Handles Button1.Click, Button2.Click
    ' this routine handles two buttons
    End Sub

    Private Sub AnotherEventHan dler(sender As Object, e As EventArgs) _
    Handles Button1.Click
    ' this routine has some additional code for Button1.Click
    End

    Both AnEventHandler & AnotherEventHan dler will be called on Button1.Click,
    while only AnEventHandler will be called for Button2.Click

    --
    Hope this helps
    Jay
    T.S. Bradley - http://www.tsbradley.net


    "dbuchanan" <dbuchanan52@ho tmail.com> wrote in message
    news:1128455175 .358485.271040@ g14g2000cwa.goo glegroups.com.. .
    | Hello,
    |
    | I can find no reason for this code to run twice. Can you help me?
    |
    | It may have something to do with it being a overrides sub but stepping
    | through during debug does *not* bear this out.
    |
    |
    | In the base class form...
    | \\
    | Protected Overridable Sub btnInsertCmptPr ofile_Click(ByV al sender As
    | System.Object, _
    | ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click
    |
    | Throw New NotImplementedE xception
    |
    | End Sub
    | //
    |
    | In the derived form...
    | \\
    | Protected Overrides Sub btnInsertCmptPr ofile_Click(ByV al sender As
    | System.Object, _
    | ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click
    |
    | 'Get profiledata from master
    | Call InsertCmptProfi le()
    |
    | 'Creates two new row objects in code and saves them to the database
    | Call InsertValveForC ylinder()
    |
    | 'Retreive new and current data
    | Call ClearAndReloadF orm()
    |
    | End Sub '<<(stepping through in debug mode) the code from here returns
    | to the top of this Sub and starts over. Why?
    | //
    |
    | While stepping through the code I discovered that it just runs the
    | derived code block twice. Why? What is the correct why to make it run
    | once?
    |
    | Thank you,
    | dbuchanan
    |


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Why does this code run twice

      dbuchanan <dbuchanan52@ho tmail.com> wrote:[color=blue]
      > I can find no reason for this code to run twice. Can you help me?
      >
      > It may have something to do with it being a overrides sub but stepping
      > through during debug does *not* bear this out.[/color]

      Have you subscribed to the event twice, perhaps?

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Why does this code run twice

        Hi,

        In addition to Jay, you have added two handlers, so remove one of those.
        Which depends in my opinion from the way that is for you normal to add an
        handler.

        I hope this helps,

        Cor


        "dbuchanan" <dbuchanan52@ho tmail.com> schreef in bericht
        news:1128455175 .358485.271040@ g14g2000cwa.goo glegroups.com.. .[color=blue]
        > Hello,
        >
        > I can find no reason for this code to run twice. Can you help me?
        >
        > It may have something to do with it being a overrides sub but stepping
        > through during debug does *not* bear this out.
        >
        >
        > In the base class form...
        > \\
        > Protected Overridable Sub btnInsertCmptPr ofile_Click(ByV al sender As
        > System.Object, _
        > ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click
        >
        > Throw New NotImplementedE xception
        >
        > End Sub
        > //
        >
        > In the derived form...
        > \\
        > Protected Overrides Sub btnInsertCmptPr ofile_Click(ByV al sender As
        > System.Object, _
        > ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click
        >
        > 'Get profiledata from master
        > Call InsertCmptProfi le()
        >
        > 'Creates two new row objects in code and saves them to the database
        > Call InsertValveForC ylinder()
        >
        > 'Retreive new and current data
        > Call ClearAndReloadF orm()
        >
        > End Sub '<<(stepping through in debug mode) the code from here returns
        > to the top of this Sub and starts over. Why?
        > //
        >
        > While stepping through the code I discovered that it just runs the
        > derived code block twice. Why? What is the correct why to make it run
        > once?
        >
        > Thank you,
        > dbuchanan
        >[/color]


        Comment

        • TrtnJohn

          #5
          RE: Why does this code run twice

          Everyone is right. The event will call all registered delagates is why you
          get the call twice. Typically it is better in the base class to implement
          eventhandlers like this:

          Private Sub btnInsertCmptPr ofile_Click(ByV al sender As System.Object, _
          ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click

          OnProfileClick( sender,e)
          End Sub

          Protected Overridable Sub OnProfileClick( ByVal sender As System.Object, _
          ByVal e As System.EventArg s)
          Throw New NotImplementedE xception
          End Sub

          Or, in your case it may even be better to declare OnProfileClick as
          MustOverride.


          "dbuchanan" wrote:
          [color=blue]
          > Hello,
          >
          > I can find no reason for this code to run twice. Can you help me?
          >
          > It may have something to do with it being a overrides sub but stepping
          > through during debug does *not* bear this out.
          >
          >
          > In the base class form...
          > \\
          > Protected Overridable Sub btnInsertCmptPr ofile_Click(ByV al sender As
          > System.Object, _
          > ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click
          >
          > Throw New NotImplementedE xception
          >
          > End Sub
          > //
          >
          > In the derived form...
          > \\
          > Protected Overrides Sub btnInsertCmptPr ofile_Click(ByV al sender As
          > System.Object, _
          > ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click
          >
          > 'Get profiledata from master
          > Call InsertCmptProfi le()
          >
          > 'Creates two new row objects in code and saves them to the database
          > Call InsertValveForC ylinder()
          >
          > 'Retreive new and current data
          > Call ClearAndReloadF orm()
          >
          > End Sub '<<(stepping through in debug mode) the code from here returns
          > to the top of this Sub and starts over. Why?
          > //
          >
          > While stepping through the code I discovered that it just runs the
          > derived code block twice. Why? What is the correct why to make it run
          > once?
          >
          > Thank you,
          > dbuchanan
          >
          >[/color]

          Comment

          • Roger Rabbit

            #6
            Re: Why does this code run twice

            If using VS.Net remember you can always use the call stack debugging window
            to review the caller code.

            RR


            "dbuchanan" <dbuchanan52@ho tmail.com> wrote in message
            news:1128455175 .358485.271040@ g14g2000cwa.goo glegroups.com.. .[color=blue]
            > Hello,
            >
            > I can find no reason for this code to run twice. Can you help me?
            >
            > It may have something to do with it being a overrides sub but stepping
            > through during debug does *not* bear this out.
            >
            >
            > In the base class form...
            > \\
            > Protected Overridable Sub btnInsertCmptPr ofile_Click(ByV al sender As
            > System.Object, _
            > ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click
            >
            > Throw New NotImplementedE xception
            >
            > End Sub
            > //
            >
            > In the derived form...
            > \\
            > Protected Overrides Sub btnInsertCmptPr ofile_Click(ByV al sender As
            > System.Object, _
            > ByVal e As System.EventArg s) Handles btnInsertCmptPr ofile.Click
            >
            > 'Get profiledata from master
            > Call InsertCmptProfi le()
            >
            > 'Creates two new row objects in code and saves them to the database
            > Call InsertValveForC ylinder()
            >
            > 'Retreive new and current data
            > Call ClearAndReloadF orm()
            >
            > End Sub '<<(stepping through in debug mode) the code from here returns
            > to the top of this Sub and starts over. Why?
            > //
            >
            > While stepping through the code I discovered that it just runs the
            > derived code block twice. Why? What is the correct why to make it run
            > once?
            >
            > Thank you,
            > dbuchanan
            >[/color]


            Comment

            Working...