executing functions from a string at run time
So I made a module with a list of functions that make my life easier. I am expirimenting with this sort of thing, and I am trying to make a little command line sort of thing to be used at run time. My question is:
Is there a way to execute internal functions like the ones I made by typing them into a text box at run time (and clicking a button or whatever)? I'm looking for something like:
Execute(command.text)
Thanks!
[453 byte] By [
subsonic] at [2007-12-21]
There's the scripting object for VBScripting, or there is the VBCodeProvider Class (There's a C# one, too) which allows you to compile and Execute VB code.
However, this may not be exactly what you are looking for: what exactly are these 'functions' that you mention (What sort of things do they do)?
you can also use system.reflection to pull method names from an assemply module and test your string against the name of the method to be invoked...
Public Function Invoke(ByVal obj As Object, ByVal
parameters() As
Object) As
Object Member of: System.Reflection.MethodBase
Summary:
Invokes the method or constructor represented by the current instance, using the specified parameters.
well, I'm not sure if that's what I want or not, I'll look into it more in a sec...
And for the functions... for example: (it's a stupid one)
Function show_message(ByVal text As String) MsgBox(text)
End Function I want the to be able to type show_message("Hello") into a text box and it come up with a message box saying hello.
First of all what you are trying to accomplish is not an easy task...you need to have a thourough understanding of the reflection namespace...You will also have to do some serious parsing of the text from the textbox...especially if parameters and functions will be included in the same text box....you could always use select case statements after parsing to run the correct function or you can even fill a combo box with the string names of your funtion and then execute accrding to what the user selects....
In either case we are at a point in which you need to make a choice of which road you would like to travel and then start down that road. Once you get stumped...come back and post the code you are stumped with and the problem you are having. You will get much better answers to your question that way!