473,395 Members | 1,577 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,395 software developers and data experts.

evaluating expressions

Is there a way to evaluate a string expression such as "55/8" to
return 6.875?
Your help is greatly appreciated.
jake
Jul 24 '08 #1
4 3959
On Jul 24, 5:17*pm, jake <jakedim...@gmail.comwrote:
Is there a way to evaluate a string expression such as "55/8" to
return 6.875?
Your help is greatly appreciated.
jake
If you're on .NET 3.5, by far the easiest way is to use the Dynamic
LINQ sample. It would look something like this:

var expr = DynamicExpression.ParseLambda(null, typeof(decimal), "55 /
8");
var method = (Func<decimal>)expr.Compile();
Console.WriteLine(method()); // 6.875

You can find the download links to it here:

You can download it from here
Jul 24 '08 #2
Thank you Pavel,
Yes I am using 3.5 and I am very familiar with LINQ and lambda
expressions. I just didn't know about this particular one. This is
exactly what I need.
Thanks again.
What would we do without these forums!!
jake
On Jul 24, 9:53 am, Pavel Minaev <int...@gmail.comwrote:
On Jul 24, 5:17 pm, jake <jakedim...@gmail.comwrote:
Is there a way to evaluate a string expression such as "55/8" to
return 6.875?
Your help is greatly appreciated.
jake

If you're on .NET 3.5, by far the easiest way is to use the Dynamic
LINQ sample. It would look something like this:

var expr = DynamicExpression.ParseLambda(null, typeof(decimal), "55 /
8");
var method = (Func<decimal>)expr.Compile();
Console.WriteLine(method()); // 6.875

You can find the download links to it here:

You can download it from here
Jul 24 '08 #3
On Jul 24, 5:53*pm, Pavel Minaev <int...@gmail.comwrote:
On Jul 24, 5:17*pm, jake <jakedim...@gmail.comwrote:
Is there a way to evaluate a string expression such as "55/8" to
return 6.875?
Your help is greatly appreciated.
jake

If you're on .NET 3.5, by far the easiest way is to use the Dynamic
LINQ sample. It would look something like this:

var expr = DynamicExpression.ParseLambda(null, typeof(decimal), "55 /
8");
var method = (Func<decimal>)expr.Compile();
Console.WriteLine(method()); // 6.875

You can find the download links to it here:
Mis-pasted the link, so here it is:

http://weblogs.asp.net/scottgu/archi...y-library.aspx

Note that this is not a part of .NET 3.5 by itself - rather, it is a
"code sample" that runs on 3.5
Jul 24 '08 #4
jake wrote:
Is there a way to evaluate a string expression such as "55/8" to
return 6.875?
You can use the JavaScript support in .NET - the code below
will work on at least 2.0 (and you can write similar code
for 1.1) unlike the dynamic LINQ solution.

Arne

==================================================

using System;
using System.Reflection;
using System.CodeDom.Compiler;

using Microsoft.CSharp;
using Microsoft.JScript;

namespace E
{
public interface IJS
{
double Eval(string expr);
}
public class JS
{
private static IJS engine;
static JS()
{
string src = "import System; import E; public class C
implements IJS { public function Eval(expr : String) : Double { return
eval(expr); } }";
CodeDomProvider comp = new JScriptCodeProvider();
CompilerParameters param = new CompilerParameters();
param.GenerateInMemory = true;
param.ReferencedAssemblies.Add("System.dll");

param.ReferencedAssemblies.Add(System.Reflection.A ssembly.GetExecutingAssembly().Location);
CompilerResults res = comp.CompileAssemblyFromSource(param,
src);
Assembly asm = res.CompiledAssembly;
engine = (IJS)asm.CreateInstance("C");
}
public static double Eval(string expr)
{
return engine.Eval(expr);
}
}
}
Jul 28 '08 #5

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

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
10
by: Jason Heyes | last post by:
Are the side effects of evaluating expr1 resolved before expr2 is evaluated? if (expr1 && expr2) Thanks.
1
by: Mike Yocum | last post by:
Using MSSCRIPT.OCX in VB6, it was easy to evaluate code that was in a string, for example "10 * 3 + 1" using the Eval method of the script control. Is such a feature available in VB .NET? ...
1
by: kaosyeti | last post by:
is there an easy way to evaluate a calculated control step by step? i have a lot of long expressions in a lot of my controls and if there's an error, tracking it down is a PAIN. excel has a...
20
by: Neroku | last post by:
Hello, i would like to know what the serious definition of statements and expressions is: i know an expression are evaluated to a value, i.e: 1 == 2 5+7 foo( 1,2) and a statement is...
4
by: pinkfloydhomer | last post by:
We might use the normal preprocessor something like this #define WIDTH 42 #define HEIGHT 100 #define AREA (WIDTH*HEIGHT) To have "42*100" is inserted in the code whereever AREA is used. But...
15
by: skibud2 | last post by:
Consider this example: u8 my_array; for (i = 0; i < (sizeof(my_array)/sizeof(u8)); i++) { ... } In the standard C specification, is the evaluation of '(sizeof(my_array)/sizeof(u8))' to 10...
11
by: Pietro Cerutti | last post by:
Hi group, here I come with a question which is quite simple per se, but for which I can't find an answer. Does the C standard guarantee that inside an expression such as (x && y) "y" is...
3
by: joeyke | last post by:
How do I go about writing a program that finds and prints the values of result for all even values of y from 2 to 16, using the following formula: result= (y-1) + (y+4)3 ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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,...

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.