Is it possible to execute a string in VB.Net (like Eval in VBScript)

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I need to be able to execute a command that is defined in a string. In
VBScript, I can do it with the Eval function. How can I do it in VB.Net?

I need something like:

Dim x As Integer
Eval("x = 3")
MsgBox x ' Should show a popup with 3 as the message

I'm in desperate need of this function. There must be something similar in
VB.Net.

Thanks in advance,
Dan
 
I need to be able to execute a command that is defined in a string. In
VBScript, I can do it with the Eval function. How can I do it in VB.Net?

VB 3.0 used to have this. It was absolutely indispensable!!!!!

There is no way that I have ever heard of to do this in VB.NET, but if you
were to invent a utility object that has a method like this:

Public Function Eval(ByVal expression As String) As Double

and it can handle math, trig, all the operators, etc...

I would buy it from you in a heartbeat.

--
Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei
 
I need to be able to execute a command that is defined in a string. In
VBScript, I can do it with the Eval function. How can I do it in VB.Net?

I need something like:

Dim x As Integer
Eval("x = 3")
MsgBox x ' Should show a popup with 3 as the message

I'm in desperate need of this function. There must be something similar in
VB.Net.

Thanks in advance,
Dan

Dan,

There is nothing in VB.NET, but there is in JScript.NET. I'm not
exactly sure if this will work for your situation or not, but you can do
something like this:

1. Create a class in JScript -

// JScript Source Code
class EvalClass
{
function Evaluate(expression)
{
return eval(expression);
}
}

2. Compile it to a dll (evaluator.dll):

jsc /target:library evaluator.js

3. Reference the dll in your VB.NET project.

4. Write Code:

Dim ec As New EvalClass()
Dim expr As String = "(10 + 6) / 8"
Dim res As Double = CType(ec.Evaluate(expr), Double)

Console.WriteLine("{0} = {1}", expr, res)

You can also pass in bits of JScript code, and have them executed :)
 
Thanks for the help.

It almost does what I want but not quite. It works fine for normal
expressions but runs into problems when the expressions use variables such
as:

Dim x, y As Integer, strExpression As String, ec As New EvalClass()
x = 3
y = 2
strExpression = "x + y"
ec.Evaluate(strExpression)

It throws an error because the x and y variables are not in the
EvalClass.Evaluate function's scope.

Any other suggestions?

Thanks,
Dan
 
Thanks for the help.

It almost does what I want but not quite. It works fine for normal
expressions but runs into problems when the expressions use variables such
as:

Dim x, y As Integer, strExpression As String, ec As New EvalClass()
x = 3
y = 2
strExpression = "x + y"
ec.Evaluate(strExpression)

It throws an error because the x and y variables are not in the
EvalClass.Evaluate function's scope.

Any other suggestions?

Thanks,
Dan

Hmmm, what about:

Dim strExpression As String
Dim x, y As Integer

x = 3
y = 2

strExpression = String.Format("{0} + {1}", x, y)
MessageBox.Show(ec.Evaluate(strExpression).ToString())

You may also be able to do something using System.Reflection.Emit?
 
* "Dan said:
I need to be able to execute a command that is defined in a string. In
VBScript, I can do it with the Eval function. How can I do it in VB.Net?

I need something like:

Dim x As Integer
Eval("x = 3")
MsgBox x ' Should show a popup with 3 as the message

I'm in desperate need of this function. There must be something similar in
VB.Net.

Samples:

<http://www.codeproject.com/useritems/evaluator.asp>
<http://www.codeproject.com/csharp/livecodedotnet.asp>

If you have a DevX account:

<http://www.devx.com/codemag/Article/10352/0/page/1>
 
I probably would not have thought of that.

Impressed !

:-)

OHM


Tom said:
Dan,

There is nothing in VB.NET, but there is in JScript.NET. I'm not
exactly sure if this will work for your situation or not, but you can
do something like this:

1. Create a class in JScript -

// JScript Source Code
class EvalClass
{
function Evaluate(expression)
{
return eval(expression);
}
}

2. Compile it to a dll (evaluator.dll):

jsc /target:library evaluator.js

3. Reference the dll in your VB.NET project.

4. Write Code:

Dim ec As New EvalClass()
Dim expr As String = "(10 + 6) / 8"
Dim res As Double = CType(ec.Evaluate(expr), Double)

Console.WriteLine("{0} = {1}", expr, res)

You can also pass in bits of JScript code, and have them executed :)

Regards - OHM# (e-mail address removed)
 
Back
Top