Executing Visual Basic code from a string

How do I execute visual basic code from a string? Here is kind of a sample of what I am doing...

Function _Function(ByVal ? As ?)
'This is where it is supposed to execute a function
?.Execute()
End Function

So can someone please help me?

[257 byte] By [optifront] at [2007-12-16]
# 1

Hi,

You can use Reflection and do something like this:


Public Sub CallMethod(ByVal strAssemblyName As String, ByVal strClassName As String, ByVal strMethodName As String)
Dim objAssembly As [Assembly]
Dim objType As Type
Dim objMethodInfo As MethodInfo
Dim objTestClass As Object
Dim objParams As Object

objAssembly = [Assembly].Load(strAssemblyName)
objType = objAssembly.GetType(strClassName)
objTestClass = Activator.CreateInstance(objType)
objMethodInfo = objType.GetMethod(strMethodName)
objMethodInfo.Invoke(objTestClass, objParams)
End Sub



Call the method as follows:
CallMethod("ClassLib", "ClassLib.TestClass", "Execute")
Regards,
Vikram

Vikram at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
Well I have a problem, again. The CallMethod() function has lots of errors. Isn't there something that is easier? Like in a program called GameMaker it is just execute_string(string). Shouldn't there be one in Visual Basic .NET 2003 also? I need help really badly, it is getting important.

Thanks,
Tanner

optifront at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
Tongue TiedNote sure what your trying to do, however here is a sample of calling a function:

Public Class MyClass
Public MyFunction(Byval MyVal as string)as string
Dim MyReturnVal As String
....Do Something
Return MyReturnVal
end Function

Public Sub Test()
Dim MyOldString as String = "Test"
Dim MyNewString as string = MyFunction(MyOldString)
end sub
end class

DMan1 at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
I am trying to execute VB code from a string. Example...


Function exCodeString(ByVal Str As String)
Execute code from string
End Function
Sub DoSomething()
exCodeString("Me.Text = ""Somestring""")
End Sub

I need a simple way to do that.

Thanks,
Tanner

optifront at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
OK you pretty much have it as simple as it gets...

try


Function exCodeString (Byval str as string) as string
Select Case str
Case = "A"
Return "This is an A"
Case = "B"
Return "This is a B"
Case Else
return "This is not an A or B"
end Select
end function

Sub DoSomething()
Me.Textbox1.text = exCodeString("A")
end sub


DMan1 at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

Yes, I need that to do it, I am using the other example for now, but I'd still like to know the textbox with executing code thing. It will improve my program by about 300% or so.

optifront at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7

Nah, you missed what he's trying to say.

Here's an example of what he wants (I don't know how to do it, I'm curious myself.): say you have a form open and a textbox and button on the form, he wants it so in the textbox you can type valid VB.NET code and execute it as it displays.

For example: In this textbox, if I type in Me.Visible = False and click the execute button, it should execute the code and then hide the form.

WilliamW at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8

Doesn't anybody know? There has to be a way to do it, last time I checked, Visual Basic .NET 2003 could do almost anything. I say almost remember. There has to be a way, because I saw a control on http://windowsforms.net that could allow you to execute VB code from a string. Please tell me an answer.

optifront at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...