|
RE: Calling subroutines by name
|
Rod Stephens
|
Apr 05, 2010 09:33 PDT
|
| | My question:
Is there a way to call these subroutines by string names such that
I can write a loop
for i=DatabaseDateCode to LatestDateCode
call "dbConvert" & i
next i
|
There are a couple ways you can do this. In VB 6, you can use
CallByName. See these examples:
http://www.vb-helper.com/howto_call_routine_by_name.html
http://www.vb-helper.com/howto_callbyname.html
In .NET you can also use CallByName like this:
Imports Microsoft.VisualBasic.CallType
Public Class Form1
' Invoke a method by name.
Private Sub btnInvoke_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnInvoke.Click
Try
CallByName(Me, txtMethodName.Text, Method, Nothing)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
' The public methods to invoke.
Public Sub Method1()
MessageBox.Show("This is Method 1")
End Sub
Public Sub Method2()
MessageBox.Show("This is Method 2")
End Sub
End Class
You can also use reflection but CallByName is easier.
|
|
 |
|