swendell
asked on
Run vb function whose name is stored in a database
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
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
ASKER
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 & ")"
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 & ")"
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
http://msdn.microsoft.com/en-us/library/22x2chfx.aspx and
http://msdn.microsoft.com/en-us/library/chsc1tx6.aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(funct ionName, 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:
https://www.experts-exchange.com/questions/22055463/is-there-any-way-to-use-refer-to-one-an-object's-properties-using-a-variable.html#17911012
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(funct
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:
https://www.experts-exchange.com/questions/22055463/is-there-any-way-to-use-refer-to-one-an-object's-properties-using-a-variable.html#17911012
ASKER
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
CallByName(Me, "MyBeep", CallType.Method, parameter)
I forgot the Me since the function is in the same area
ASKER
Thank you
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.