Welcome Guest!
 VB Helper Q & A
 Previous Message All Messages Next Message 
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:


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
            CallByName(Me, txtMethodName.Text, Method, Nothing)
        Catch ex As Exception
        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.
 Previous Message All Messages Next Message 
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Food & Drink
 Health & Fitness
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Travel & Leisure
 Women & Family

  Start Your Own List!
Email lists are great for debating issues or publishing your views.
Start a List Today!

© 2001 Topica Inc. TFMB
Concerned about privacy? Topica is TrustE certified.
See our Privacy Policy.