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...

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

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.

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

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...

Edited 6 Years Ago by dor.marchan: n/a

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

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

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

This question has already been answered. Start a new discussion instead.