Executing a text representation of a Function.

  • Thread starter Thread starter Byron Hopp
  • Start date Start date
B

Byron Hopp

Is this possible in Vb.Net?

Dim strFuncName as String
Dim strFuncParm as String
strFuncName = "MyFunc(strFuncParm)"
strFuncParm = "Hello World"

Can I call a function which when I pass strFuncName to it, it will execute
the function MyFunc()?

Function MyFunc(strFuncParm as String)
msgbox strParm
End Function

Byron...
 
Byron said:
Is this possible in Vb.Net?

Dim strFuncName as String
Dim strFuncParm as String
strFuncName = "MyFunc(strFuncParm)"
strFuncParm = "Hello World"

Can I call a function which when I pass strFuncName to it, it will execute
the function MyFunc()?

Function MyFunc(strFuncParm as String)
msgbox strParm
End Function

Like the magic 'Eval()' function in BASICs of times long past? The
short answer is "no" - because at run-time you're not running Basic
code, you're running IL, and something that understands VB (eg a VB
compiler) is far far away.

The slightly longer answer is "yes, if you cheat". VB*Script* has such
a function (Eval()) - so find a Script host in your runtime environment
and persuade it to do what you want...?

Another longer answer is "yes, if you're prepared to work at it". With
the powerful and arcane wizardry known as Reflection, you have runtime
access to all the bits and pieces that make up your program. So find a
procedures called "MyFunc", look at its list of arguments, prepare
those arguments, and Invoke() and you're there.

Finally, I think I saw some code once where IL was dynamically
generated and executed at run time, all from managed code. Can't
remember where or how now, but it's a thought...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top