calling sub question

  • Thread starter Thread starter djc
  • Start date Start date
D

djc

I have a subroutine that is used in a few places within a page. It is
declared with no parameters like so:

Sub subName()

End Sub

I would now also like to call this same sub from a button click using
OnClick or OnComand. However each of those require there own signature such
as:

Sub SubName(sender As Object, e As System.EventArgs) etc..

End Sub

If I don't have the right signiture I get a compile error. Then, if I add
the signature I get a compile error when this sub is called from other
places besides the button's event.

error with signature:
Argument not specified for parameter 'e' of 'Public Sub
ResetDynamicControls(Sender As Object, e As System.EventArgs)

error without signiture:
Method 'Public Sub ResetDynamicControls()' does not have the same signature
as delegate 'Delegate Sub EventHandler(sender As Object, e As
System.EventArgs)'

How can I use/call the same subroutine both from a button's event AND other
places such as within other subroutines?
 
Write a sub that does what you need to do. Call that sub from the event
handler. For example:

if your sub is:

Sub ResetDynamicControls()
//Code goes in here
End Sub

then your event handler looks like:

Sub Button_OnClick(sender As Object, e As System.EventArgs) etc..
ResetDynamicControls()
End Sub
 
I think the easiest way to resolve this is to place a call to the subroutine
"Subname" from inside the Sub that handles the button.Click event.

For example:

' This is linked to the button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Subname()
End Sub

' This is the original subroutine
Sub Subname()
....
End Sub

Ray
 
Ahh. Yeah. Nice. Thanks!

Ben Lucas said:
Write a sub that does what you need to do. Call that sub from the event
handler. For example:

if your sub is:

Sub ResetDynamicControls()
//Code goes in here
End Sub

then your event handler looks like:

Sub Button_OnClick(sender As Object, e As System.EventArgs) etc..
ResetDynamicControls()
End Sub

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com
 
will do. Thank you.

Raymond U. Holder said:
I think the easiest way to resolve this is to place a call to the subroutine
"Subname" from inside the Sub that handles the button.Click event.

For example:

' This is linked to the button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Subname()
End Sub

' This is the original subroutine
Sub Subname()
...
End Sub

Ray
 
Back
Top