Solved

Run vb function whose name is stored in a database

Posted on 2008-10-04
7
459 Views
Last Modified: 2012-05-05
I have a data table with Function names.
These functions are defined in my code and work properly.
Most of these functions return a value I require.
I can retrieve the function name from the database but I don't know to invoke the function in vb.net

Should I be retrieving the function name from the database into a string field ?
How would I run/execute the function with parameters
Can you provide a small example
0
Comment
Question by:swendell
7 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22641869
If your table contains data like this:

FunctionName, Param1, Param2, Param3

What you could do is retrieve the values from database as strings or if parameters are numbers as integers.  Then use a case logic or if statement to determine which function to call.

If dr("FunctionName") = "MyFunction1" Then
    MyFunction1(dr("Param1"), dr("Param2"), dr("Param3"))
End If

Where dr is your SqlDataReader for example, but however you retrieve data is inconsequential to my point so hopefully it is clear.
0
 

Author Comment

by:swendell
ID: 22641906
I can retrieve function name into a string but then I want to run that function.
BUT keep in mind I have many functions and I am not hard-coding every possible function name/value as you did in the example above. In concept looking for something more like this:

Dim myFunction = FunctionNameFromDatabase as string
Execute myFunction & "(" & parameter1 & ")"

0
 
LVL 2

Expert Comment

by:madivad2
ID: 22641919
You are after the "CallByName" Function... refer these links:

http://msdn.microsoft.com/en-us/library/22x2chfx.aspx    and
http://msdn.microsoft.com/en-us/library/chsc1tx6.aspx
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 2

Accepted Solution

by:
madivad2 earned 300 total points
ID: 22641948
Note: Your function or sub MUST be public!

Some sample code for good measure:
Private Sub button_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CallByName(Me, "MyBeep", CallType.Method)
End Sub
 
Public Sub MyBeep()
    Beep()
End Sub

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22642008
CallByName() will work fine...but if you want the ".Net approved" method then you'll want to use Reflection.  The method/function doesn't have to Public with this approach:

Imports System.Reflection
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strIn As String = "Idle_Mind"
        Dim retValue As Integer = 0
        Dim functionName As String = "Foo"

        Dim mi As MethodInfo = Me.GetType.GetMethod(functionName, BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic)
        retValue = mi.Invoke(Me, New Object() {strIn})
        MessageBox.Show("retValue = " & retValue, "Return value of Foo()", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Function Foo(ByVal param As String) As Integer
        MessageBox.Show("param = " & param, "Inside Foo()", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Return IIf(param.ToLower = "idle_mind", 1, 0)
    End Function

End Class


Some more examples:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22055463.html#17911012
0
 

Author Comment

by:swendell
ID: 22642108
madivad2: That was it. I only needed to add a comma and insert the parameter like this:
CallByName(Me, "MyBeep", CallType.Method, parameter)
I forgot the Me since the function is in the same area
0
 

Author Closing Comment

by:swendell
ID: 31503080
Thank you
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 49
VB: Convert 2 dates to specific format 24 52
FInd Image Control Gridview 3 21
VB.NET Application Installation with sqlserver 8 30
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question