Why does this code run twice

D

dbuchanan

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 btnInsertCmptProfile_Click(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) Handles btnInsertCmptProfile.Click

Throw New NotImplementedException

End Sub
//

In the derived form...
\\
Protected Overrides Sub btnInsertCmptProfile_Click(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) Handles btnInsertCmptProfile.Click

'Get profiledata from master
Call InsertCmptProfile()

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

'Retreive new and current data
Call ClearAndReloadForm()

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
 
J

Jay B. Harlow [MVP - Outlook]

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 btnInsertCmptProfile_Click as Overridable I would simply
have an overridable OnInsertCmptProfile that the event handler calls. This
hides (encapsulates) that an event is involved, which allows
OnInsertCmptProfile to possibly be called in other cases...

| In the base class form...
| \\
| Private Overridable Sub btnInsertCmptProfile_Click(ByVal sender As
| System.Object, _
| ByVal e As System.EventArgs) Handles btnInsertCmptProfile.Click
OnInsertCmptProfile()
End Sub

Protected Overridable Sub OnInsertCmptProfile ()
| Throw New NotImplementedException
| End Sub

| In the derived form...
| \\
| Protected Overrides Sub OnInsertCmptProfile()
...
| 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 AnotherEventHandler(sender As Object, e As EventArgs) _
Handles Button1.Click
' this routine has some additional code for Button1.Click
End

Both AnEventHandler & AnotherEventHandler 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


| 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 btnInsertCmptProfile_Click(ByVal sender As
| System.Object, _
| ByVal e As System.EventArgs) Handles btnInsertCmptProfile.Click
|
| Throw New NotImplementedException
|
| End Sub
| //
|
| In the derived form...
| \\
| Protected Overrides Sub btnInsertCmptProfile_Click(ByVal sender As
| System.Object, _
| ByVal e As System.EventArgs) Handles btnInsertCmptProfile.Click
|
| 'Get profiledata from master
| Call InsertCmptProfile()
|
| 'Creates two new row objects in code and saves them to the database
| Call InsertValveForCylinder()
|
| 'Retreive new and current data
| Call ClearAndReloadForm()
|
| 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
|
 
J

Jon Skeet [C# MVP]

dbuchanan said:
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.

Have you subscribed to the event twice, perhaps?
 
C

Cor Ligthert [MVP]

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
 
G

Guest

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 btnInsertCmptProfile_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnInsertCmptProfile.Click

OnProfileClick(sender,e)
End Sub

Protected Overridable Sub OnProfileClick(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Throw New NotImplementedException
End Sub

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

Roger Rabbit

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

RR
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top