Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute code from string 1

Status
Not open for further replies.

VDavid

Programmer
Sep 14, 2003
118
VE
Hi,

I was wondering if there is a way to execute code in VB.NET from a string. I'm using VB.NET 2008 Express.

For example, read a file or database and put it in a string then execute the command in runtime.
Code:
Dim StrCommand as String
strCommand = "Msgbox(""Hello World"")"
Execute(strCommand)
I'm using an example, the final code will be getting the command from a database, check and split if there is more than one command and then execute all lines one by one.

Thanks in advance.
 
You could use the Microsoft Script Control. To use this control, go to Project->Add Reference. In the dialog that opens select the COM tab, scroll down to the Microsoft Script Control 1.0, select the control and click the Select button. Click OK to accept the reference and close the dialog. Then in your code:

Dim StrCommand As String
StrCommand = "Msgbox(""Hello World"")"

Dim sc As New MSScriptControl.ScriptControl

sc.Language = "VBScript"

sc.Eval(StrCommand)



There are other ways to do this, but IMHO this is the easiest.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank you for answering me jebenson

But as I understand, that will only work with if the commands are in vbscript, I'm looking for a way to execute VB.NET code lines. I was using the message box as an example, but I would like to interact with the .net program, changing variables values or executing procedures, something like
Code:
Me.Close()
or
Code:
BtnClose_Click(sender, e)

Any thoughts?

 
I would just say to be sure to consider other options thoroughly. This will be very difficult to debug and kind of reduces your Visual Basic application to not much more than a scripting host. I would recommend to anyone to strive to build robust applications when developing in .Net.
 
Hi,
Sorry for the delay...

jebenson
Thanks for the links, those are very useful.
I don't know how I miss them, guess I didn't use the right search keywords :)

RiverGuy
Thanks for your advice. I'll take them in consideration




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top