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!




Answer this question

executing functions from a string at run time

  • iiiwabibitoiii

    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.



  • kanuhelp

    Yes, I'd start with the CallByName functionality before delving into the others mentioned. It may do precisely what you need.

  • carlisle

    I'm sorry, but I don't understand fully. I get what it means, but I don't know how to do it!


  • P K Agarwal

    You may want to look at the CallByName function, it hides a lot of the reflection details from you:

    http://msdn2.microsoft.com/en-us/chsc1tx6.aspx

    Hope that helps,

    Jonathan Aneja

    The VB Team



  • Dave Midgley

    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!



  • Stephen S.

    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)



  • Donal McWeeney

    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.



  • executing functions from a string at run time