473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

run function from string

Hi,

I've a question. I'm developing a windows application and for the
application i need to run functions and procedures that are stored in a
database.
Here is an example that i tried to get working.
How can i execute the function that is stored in the string expr ?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
  2. System.EventArgs) Handles Button1.Click
  3. Dim expr As String = "test2(10,25)"
  4. MessageBox.Show(expr)
  5. expr.run()
  6. End Sub
  7.  
  8. Function test2(ByVal i As Integer, ByVal x As Integer) As Boolean
  9. Dim y As Integer
  10. y = i + x
  11. Return y
  12. End Function
  13.  
Mar 6 '07 #1
3 1836
You will have to write code that parses the text and calls the appropriate
function.

--
HTH,

Kevin Spencer
Microsoft MVP

Help test our new betas,
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Jan Heppen" <Ja*******@discussions.microsoft.comwrote in message
news:D1**********************************@microsof t.com...
Hi,

I've a question. I'm developing a windows application and for the
application i need to run functions and procedures that are stored in a
database.
Here is an example that i tried to get working.
How can i execute the function that is stored in the string expr ?

Expand|Select|Wrap|Line Numbers
  1.    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
  2. System.EventArgs) Handles Button1.Click
  3.        Dim expr As String = "test2(10,25)"
  4.        MessageBox.Show(expr)
  5.        expr.run()
  6.    End Sub
  7.    Function test2(ByVal i As Integer, ByVal x As Integer) As Boolean
  8.        Dim y As Integer
  9.        y = i + x
  10.        Return y
  11.    End Function
  12.  

Mar 6 '07 #2
Imports System.Text.RegularExpressions
Imports System.Reflection

Module Module1
Class Expression
Private Shared methodRegex As Regex = New
Regex("^(?<Method>\w[\w\d_]+)\s*(\((?<Parameters>.*)\))?$",
RegexOptions.Compiled Or RegexOptions.Singleline)
Public Shared Function Run(ByVal obj As Object, ByVal expr As
String) As Object
Dim m As Match = methodRegex.Match(expr)
Dim method As String = m.Groups("Method").Value
Dim parameters As String = m.Groups("Parameters").Value
Dim params As Object() = Nothing

Dim binding As BindingFlags = BindingFlags.InvokeMethod Or
BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public Or
BindingFlags.NonPublic

Dim mi As MethodInfo = obj.GetType().GetMethod(method, binding)

If mi Is Nothing Then
Return Nothing
End If

If Not String.IsNullOrEmpty(parameters) Then
Dim stringParams As String() = parameters.Split(New String()
{","}, StringSplitOptions.RemoveEmptyEntries)
ReDim params(stringParams.Length - 1)

Dim parameterInfo As ParameterInfo() = mi.GetParameters()

If params.Length <parameterInfo.Length Then
Return Nothing
End If

For Each pi As ParameterInfo In parameterInfo
params(pi.Position) =
Convert.ChangeType(stringParams(pi.Position), pi.ParameterType)
Next
End If

Return mi.Invoke(obj, binding, Nothing, params,
Threading.Thread.CurrentThread.CurrentCulture)
End Function
End Class

Sub Main()
Dim expr As String = "test2(10,25)"
Expression.Run(New TestClass, expr)
End Sub

Class TestClass
Public Function test2(ByVal i As Integer, ByVal x As Integer) As
Integer
Dim y As Integer
y = i + x
Return y
End Function
End Class
End Module
"Jan Heppen" wrote:
Hi,

I've a question. I'm developing a windows application and for the
application i need to run functions and procedures that are stored in a
database.
Here is an example that i tried to get working.
How can i execute the function that is stored in the string expr ?

Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
  2. System.EventArgs) Handles Button1.Click
  3.         Dim expr As String = "test2(10,25)"
  4.         MessageBox.Show(expr)
  5.         expr.run()
  6.     End Sub
  7.     Function test2(ByVal i As Integer, ByVal x As Integer) As Boolean
  8.         Dim y As Integer
  9.         y = i + x
  10.         Return y
  11.     End Function
  12.  
Mar 6 '07 #3
I assume you already have the 'test2' function in the same class from where
you want to invoke it.
You can invoke the function in the expr as follows

Dim expr As String = "test2"

Dim obj As Object = Activator.CreateInstance(Me.GetType(), New String() {})

Dim result As Object = Me.GetType.InvokeMember(expr,
Reflection.BindingFlags.Default Or Reflection.BindingFlags.InvokeMethod,
Nothing, Me, New Object() {10, 25})

MessageBox.Show(CType(result, Integer).ToString())

Note that I have passed your parameters (10, 25) as the final parameter to
the InvokeMember function which is a param array. Also correct your test2
function, I think it is required to return an integer whereas the return
type is boolean.
--
Abdul Aleem
Blog: http://aleemkhan.wordpress.com

"Jan Heppen" <Ja*******@discussions.microsoft.comwrote in message
news:D1**********************************@microsof t.com...
Hi,

I've a question. I'm developing a windows application and for the
application i need to run functions and procedures that are stored in a
database.
Here is an example that i tried to get working.
How can i execute the function that is stored in the string expr ?

Expand|Select|Wrap|Line Numbers
  1.    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
  2. System.EventArgs) Handles Button1.Click
  3.        Dim expr As String = "test2(10,25)"
  4.        MessageBox.Show(expr)
  5.        expr.run()
  6.    End Sub
  7.    Function test2(ByVal i As Integer, ByVal x As Integer) As Boolean
  8.        Dim y As Integer
  9.        y = i + x
  10.        Return y
  11.    End Function
  12.  

Mar 7 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
18
by: James Radke | last post by:
Hello, We are currently using a user DLL that when working in VB 6.0 has a user defined type as a parameter. Now we are trying to use the same DLL from a vb.net application and are having...
2
by: pangel83 | last post by:
I've been trying for days to write a piece of VB.NET code that will read from winamp's memory space the paths of the files from the current winamp playlist. The GETPLAYLISTFILE command of the...
4
by: Paul | last post by:
Anyone have code that emulates the Nz function in Microsoft Access? In Access it is: Nz(Value as variant, Optional ValueIfNull as Variant) as Variant
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
20
by: Andrew Morton | last post by:
Is it possible to have two function declarations which take the same parameters but return different types depending on how the function is used? function f(x) as string ' return a string end...
9
by: =?Utf-8?B?RGFya21hbg==?= | last post by:
Hi, I am wondering how you multi-dimension an array function? My declared function looks like this: Public Function GetCustomerList(ByVal Val1 As String, ByVal Val2 As Long, ByVal Val3 As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.