473,759 Members | 7,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to evaluate in C# a string of expression

Hello,
Please help!!! I've been stuck on this issue for months. I just wonder if
there is a way to programmaticall y evaluate expression
strings such as
( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 ) >
( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment or
advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment and
advise.

Zeng

Nov 16 '05 #1
13 33863
Try this:

textBox1.Text = (3 + 5 / 2 > 4).ToString();
textBox2.Text = ((3 + 5) / 2 > 4).ToString();
"Zeng" <Ze******@hotma il.com> wrote in message
news:uG******** ******@TK2MSFTN GP09.phx.gbl...
Hello,
Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmaticall y evaluate expression
strings such as
( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 )

( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment or advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment and advise.

Zeng

Nov 16 '05 #2
I can think of at least three ways to do this without rolling your own
interpreter.

(a) You can invoke the C# compiler on it and compile into memory. This
approach requires you to be extremely confortable with reflection in order
to instantiate and talk to the object you create.

(b) You can host the Windows Scripting Host engine and use javascript or
VBScript (or perl or whatever rings your bell) to parse and evaluate
expressions.

(c) You can pay me to write you a component that does (a) or (b).
Nov 16 '05 #3
Zeng,

I still did not try this one.

\\\Eval by Nigel Amstrong
1. Create a file called: DynamicMath.js
2. Add this code to it:

class DynamicMath
{
static function Eval(MathExpres sion : String) : double
{
return eval(MathExpres sion);

};
}

3. Compile it with the command line jsc compiler: jsc /t:library
DynamicMath.js
4. Add a reference to DynamicMath.dll to your project (and to
Microsoft.JScri pt.dll as well)
5. Use from your favourite .NET language:
Dim d As Double = DynamicMath.Eva l("2 + 3 + 4")
MessageBox.Show (d)
6. That's it..
///

Maybe you can do

Cor
"Zeng" <Ze******@hotma il.com> schreef in bericht
news:uG******** ********@TK2MSF TNGP09.phx.gbl. ..
Hello,
Please help!!! I've been stuck on this issue for months. I just wonder
if
there is a way to programmaticall y evaluate expression
strings such as
( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 )
>

( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment
or
advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment
and
advise.

Zeng

Nov 16 '05 #4
"Zeng" <Ze******@hotma il.com> wrote in
news:uG******** ********@TK2MSF TNGP09.phx.gbl. ..
Hello,
Please help!!! I've been stuck on this issue for months. I just wonder
if
there is a way to programmaticall y evaluate expression
strings such as
( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 )
>

( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment
or
advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment
and
advise.


If you're only trying to evaluate simple mathematical expressions, building
your own parser might be an option: Most parser generators (like ANTLR) come
with a "calc"-sample already, it's usually not much work to add one or two
operators.

Niki
Nov 16 '05 #5
Hi Zeng,
try
1. info.lundin.Mat h from http://www.lundin.info/
2. Complex & DateTime Parser assembly for .NET from
http://www.geocities.com/alexei_cioina/cioinaeval.html

"Zeng" wrote:
Hello,
Please help!!! I've been stuck on this issue for months. I just wonder if
there is a way to programmaticall y evaluate expression
strings such as
( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 ) >
( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment or
advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment and
advise.

Zeng

Nov 16 '05 #6
Zeng wrote:
Hello,
Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmaticall y evaluate expression
strings such as
( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 ) > ( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment or advice.

There was an old post fro Scott Alen but I couldn't follow what he refered to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment and advise.

Zeng

Hi Zeng,

You can do this with the DotBasic runtime, which is completely free.

In the download package there is a CSharp source file showing how to do
inline evaluation.

Here are the relevent lines:

....
using DotBasicRuntime ;
private Runtime runtime;
....
runtime = new Runtime();
object result = runtime.Eval(in putText.Text);
....

Testing the examples you posted:

runtime.Eval("( ( 3 + 5 ) / 2 ) > 4") returned False

runtime.Eval("( ( 3 + 6 ) / 3 ) > ( ( 5 + 3 ) / 4 )") returned True
The Eval statement can handle any legal DotBasic expression or program.
http://dotbasicscript.com

Regards,

-- Peter Fisk

Nov 16 '05 #7

I think the easiest way to do is to create a jscript.net class with eval
function and use jsc.exe to compile it as library dll and then reference it
in your C# project.

John
"Zeng" <Ze******@hotma il.com> wrote in message
news:uG******** ********@TK2MSF TNGP09.phx.gbl. ..
Hello,
Please help!!! I've been stuck on this issue for months. I just wonder
if
there is a way to programmaticall y evaluate expression
strings such as
( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 )
>

( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment
or
advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment
and
advise.

Zeng

Nov 16 '05 #8
Zeng wrote:
Hello,
Please help!!! I've been stuck on this issue for months. I just wonder
if there is a way to programmaticall y evaluate expression
strings such as
( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 )

( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment
or advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment
and advise.

Zeng

Look in the compiler access. It is possible to compile on the fly.
Nov 16 '05 #9
Zeng wrote:
Hello,
Please help!!! I've been stuck on this issue for months. I just wonder
if there is a way to programmaticall y evaluate expression
strings such as
( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 )

( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment
or advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment
and advise.

Zeng

Another easier solution:
1. make an ascx file with language="jscri pt"
2. make a function in this that says evaluate with one parameter sExpression
the expression as a string
3. in the function say
return eval(sExpressio n);

That should do it.
Thanks
Nov 16 '05 #10

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

Similar topics

9
1878
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button" element.onfocus = function() {FocusIsOn = "Button_1"; alert(FocusIsOn)} That works great. If the button gets the focus, I got a message box saying 'Button_1'. However, if I set the variable 'FocusIsOn' dynamically (using a variable
3
5692
by: Jason luo | last post by:
Hi all, In c99-standard page 52,there is a sentence about void,as below: If an expression of any other type is evaluated as a void expression, its value or designator is discarded. I don't know how to understand it, How to evaluate the expression as a void expression? explicit conversion the expression? but, befor the sentence ,"implicit or explicit conversions (except to void) shall not
2
1672
by: EDom | last post by:
Hi, I have expressions in a string like "100/24*5" and so on. How can I evaluate this expression to get the result. Regards, Vineet
2
1442
by: Ray Martin | last post by:
I have created a custom datagridcolumn, and would like to color the cell based on criteria passed as a parm when the column is created. I would like this to be generic so I want to do something similar to the Clipper/dBase function "&", or even the DOTNET Debug.Evaluate methid. So, for example if I wanted the cell in row 5 to be the one to be colored differently I could do something like: mynewcolumn = new...
8
13128
by: No Such Luck | last post by:
Is there anyway to literally evaluate the contents of a string in an if statement? For example: int i = 0; char * str = "i == 0"; if(str) /* I know this doesn't do what I want */ {
4
3582
by: netnet | last post by:
Is it possible to evaluate a string as a method name? Something like this: Session = "loadFormFormACompany"; This is what I would like to do.... eval(Session+"()") Anyone have any ideas?
4
1977
by: Wilson | last post by:
Hello, How can i eval a string expression like String abc = ??"dt.Rows.Columns.ToString() + dt.Rows.Columns.ToString()" Thanks Wilson
2
2161
by: parul.prasad | last post by:
How can we evaluate a string expression in if statement Linux C/C++
2
5082
by: yarborg | last post by:
This is kind of a weird one and hard to find answers online because of the format of the question. Essentially I want to be able to have a string that looks like this "True AND True AND True" and evaluate that to a boolean which in this case would be True. Another example would be "(True or False) AND True" would be True. The reason for this is that I have a form where the user has to build some logic in a graphical interface. I can evaluate...
1
11059
by: Monusonu | last post by:
Hi Expert, I am trying to get the value from excel formula cell using POI. My code works fine for less complex formula cells, but fails or returns error code for complex formula cells. Following is the code I am using to get values:- public class POIMain{ public POIMain() { } public static void main(String args) { try{
0
9521
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9333
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9900
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8768
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7324
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.