Question Calling a function from a string

Ozaddy

New member
Joined
Jun 2, 2010
Messages
1
Programming Experience
Beginner
Hi,

I have an XML file with a list of tasks to be executed. One of the elements of the XML file is funcName (function name), which is the name of a function to be executed in my Visual Basic.NET application (v4).

I can retrieve the string name of the function, but how do I go about running it? I have two Class files, GUI and DISP.

i.e.
myDISP AS new DISP
strFunction = readXML()
myDISP.strFunction (where strFunction is the name of the function returned from the XML file.)

Thanks in advance.

Adam
 
Unfortunately there is no way of using a string to run a function directly.

The only method that you could possibly use is to create a function which takes that function name and then has a select case, which then calls the function itself.

So something like
VB.NET:
Select Case strFunction
    Case "MsgBox"
        MsgBox("Hi")
    Case "Exit"
        Application.Exit()
End Select

Obviously it would be possible for you to do something more elaborate with parameters, but this should give you an idea of one way that you could go about doing this.

Hope this helps

Satal :D
 
Either that or use Reflection:
VB.NET:
GetType(DISP).GetMethod("methodname").Invoke(myDISP, Nothing))
 
Back
Top