Hi All,

I'm a student who really need help in this problem...what I need to do is to call/run/execute a subroutine using a varaible name from a subroutine.

Public Sub Main(ByVal subName as String)
Call subName  ' subName may be contain the value AAA or BBB
End Sub
'===========================
Public Sub AAA()
'do something for AAA
End Sub
'===========================
Public Sub BBB()
'do something for BBB
End Sub

I searched the web by no luck of trying what they have suggested or I have not found the right syntax...Please help...thanks in advance...

My experience is that the following is more robust:

Public Sub Main(ByVal subName as String)

   Select Case subName.Trim.ToLower 'Comparison always with lowercase text, no spaces
      Case "aaa"
          AAA()
      Case "bbb"
          BBB()
      Case Else
          Msgbox ("Tried to call an undefined subroutine.")
          'Other trapping of undefined routine calls are also possible
          'Or use the Case Else to define a default routine.
   End select

End Sub

If you like, feel free to visit my Visual Basic.net developers website for more tips on programming in Visual Basic.net.

My experience is that the following is more robust:

Public Sub Main(ByVal subName as String)

Select Case subName.Trim.ToLower 'Comparison always with lowercase text, no spaces
Case "aaa"
AAA()
Case "bbb"
BBB()
Case Else
Msgbox ("Tried to call an undefined subroutine.")
'Other trapping of undefined routine calls are also possible
'Or use the Case Else to define a default routine.
End select

End Sub

If you like, feel free to visit my Visual Basic.net developers website for more tips on programming in Visual Basic.net.

===========================================

I thought of that but the real issue I have is I will be calling almost 900 subroutines...this will make 900 Case statements, which I am trying to avoid.

Also, all subroutines are in Modules (not within Forms). I found something that would do it but it call a form and not a subroutine within a Module. I also found something but it is written ASP.Net and not in VB.Net application; unfortunately, I cant convert it by my own.

Thanks in advance again for your help...

Well, that complicates things a bit, but it is possible. Th trick is to define a class, containing all your routines. Below I give a simplified example.

Public Class CallingDemo

   Public Sub Aaa(ByVal s As String)
        MsgBox("Called Aaa with parameter=" & s)
   End Sub

   Public Sub Bbb(ByVal s As String)
      MsgBox("Called Bbb with parameter=" & s)
   End Sub

   Public Sub Ccc(ByVal s As String)
       MsgBox("Called Ccc with parameter=" & s)
    End Sub

End Class

Now, call these these routines with the following code:

Public Sub CallASub(ByVal sSubroutine As String, ByVal sSomeParameter As String)

        Dim MyObj As New CallingDemo

        Dim myType As System.Type = GetType(CallingDemo)

        Dim myInfo As System.Reflection.MethodInfo = myType.GetMethod(sSubroutine)

        Dim myParameters() As Object = {sSomeParameter}

        myInfo.Invoke(MyObj, myParameters)[/INDENT]

    End Sub

You can call this function with the following:

CallASub("Aaa", "Test")

Be aware of upper- and lowercase for the function names!

Hope this helps!

This solution is also written in the article Calling subroutines by variable name in Visual Basic.net

Hi,

First shift all the procedures to the "Class" object.(say MyClass)
and in calling form, use this code:

Dim TCls As New MyClass
Dim TempName as String
TempName="AAA"
CallByName(TCls, TempName, CallType.Method)
TCls = Nothing

Regards
Veena

Hi, I used the advice of QVeen72. Thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.