473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.548 / Virus Database: 341 - Release Date: 12/5/2003
Nov 20 '05 #1
6 23906
> 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
Nov 20 '05 #2
On 2003-12-10, Dan <dh******@somewhere.com> wrote:
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
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.548 / Virus Database: 341 - Release Date: 12/5/2003


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 :)
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #3
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
"Tom Shelton" <to*@mtogden.com> wrote in message
news:eg**************@tk2msftngp13.phx.gbl...
On 2003-12-10, Dan <dh******@somewhere.com> wrote:
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
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.548 / Virus Database: 341 - Release Date: 12/5/2003


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 :)
--
Tom Shelton
MVP [Visual Basic]

Nov 20 '05 #4
On 2003-12-10, Dan <dh******@somewhere.com> wrote:
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).ToStrin g())

You may also be able to do something using System.Reflection.Emit?
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #5
* "Dan" <dh******@somewhere.com> scripsit:
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>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
I probably would not have thought of that.

Impressed !

:-)

OHM
Tom Shelton wrote:
On 2003-12-10, Dan <dh******@somewhere.com> wrote:
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
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.548 / Virus Database: 341 - Release Date: 12/5/2003


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# On**********@BTInternet.com
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Arvid Andersson | last post by:
Hello, I need to convert a string to a number, but the string can contain +,-,* and / as well as parenthesis. For example, if I have the string "30/(6+9)" I would like a function that returned the...
5
by: KB | last post by:
Hi, This may be a rudimentary question: How to convert a string like '777' to an octal integer like 0777, so that it can be used in os.chmod('myfile',0777)? I know the leading zero is...
1
by: Mike | last post by:
Help, I am using an encryption routine that occasionally will encrypt a string using some extended ASCII characters (ASCII code > 128) I am wondering if there is a reserved character in VB that...
19
by: linzhenhua1205 | last post by:
I want to parse a string like C program parse the command line into argc & argv. I hope don't use the array the allocate a fix memory first, and don't use the memory allocate function like malloc....
3
by: Eric Newton | last post by:
Given databinding an array of System.Version types: Given that "SomeObject" type has a Version property: public class SomeObject { public Version Version { get; } public string Description {...
1
by: TJS | last post by:
in vbscript there was a command called "execute" which would process a dynamic string vb.net dropped that feature ... does anybody have a solution for how to execute a string in vb.net through...
4
by: Akihiro KAYAMA | last post by:
Hi all. I would like to ask how I can implement string-like class using tuple or list. Does anyone know about some example codes of pure python implementation of string-like class? Because I...
2
by: | last post by:
Hello, I have a php file with php and html code. It looks like this: <html...<? ... ?</html> If I had this file on disk then I could just include it, but I have this file in string. When I...
3
by: Jerzy Zielinski | last post by:
Hi all, I would like to execute/evaluate a string that contains a VB script, for example: Dim s as string = "Dim avar as integer = 1" ' Evaluate s... so s can be executed during runtime and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.