473,325 Members | 2,785 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,325 software developers and data experts.

What is C#. NET equivalent of Javascript's eval() ?

7
I have a programs with thousands of variables. I need to perform complex operations on each of these variables that involves other variables.

What I would like to do is create a two column spreadsheet containing each of the variable names and the formula (stored as a string) used to calculate that variable's value. For each variable in the C# program, I would look up the string containing the formula and then calculate that formula.

Simple example:
Let's assume that there are three variables: x, y, and z.

x = 10
y = 20

I look up the formula for z and it returns the following string:

string = "x + y"

I am looking for a function that would do the equivalent of:

z = eval(string)
with the result that z would equal 30.

How can I do this in C#?

Many thanks in advance for any help.
Sep 12 '10 #1
5 3971
Elliot
7
One further clarification:

Some of the formulas that I will using will call methods that I have defined.

So the string for z may be "x + y", but it might also be "NewMethod(x,y)"
Sep 13 '10 #2
Plater
7,872 Expert 4TB
If it can be done, it will probably involve System.Reflection
Sep 13 '10 #3
Elliot
7
Thanks for replying to my question. I looked up System.Reflection but have no idea how to implement it to solve this problem. Do you have a suggestion of how this System.Reflection might help in this case?
Sep 13 '10 #4
Elliot
7
Perhaps it would be helpful if I were to try to clarify what I'm looking to do:

Let's assume that we have a program with the following variables:

double x1 = 20;
double x2 = 30;
double y = 100;
string s = "y + NewMethod(x1, x2)";


Furthermore, let's assume that NewMethod() is some method that performs a calculation on its two parameters and returns a value of type double. For example, let's assume that NewMethod(20,30) would produce 610.23 as a result.

I am looking for a method (let's call it "Formula()" for demonstation purposes), such that:

z = Formula(s);

would produce the same result as:

z = y + NewMethod(x1, x2);

(i.e. z would be equal to 710.23)
Sep 13 '10 #5
Alex Papadimoulis
26 Expert
There is no "easy" way to do this in C#.

C# (and other .NET languages) are compiled into MSIL, and the Common Language Runtime (which executes MSIL) has no understanding of C#, VB, or any other .NET language. Interpreted languages (like JavaScript) have runtime engines that can understand and execute code.

That said, there are a few ways to handle this:

Write a parser/compiler for your specific needs - there are several tools (including the great GOLD Parsing System that can do all of the heavy lifting.

Use Windows Scripting Host - this is obviously Windows-specific, but it will enable you to execute Java or VB script and pass in your own objects. See msdn for more info.

Compile at run-time - this can get very tricky; there are plenty of helper classes within Reflection.Emit that can help you out.
Sep 13 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Dark Cowherd | last post by:
http://www.ucalc.com/mathparser/index.html There is a great library called UCALC which allows you to set up an expression and evaluate it for e.g. you an define an expression by calling a...
2
by: Mark Richards | last post by:
Perhaps this has been covered elsewhere, but can't find a reference to it in FAQ and through diligent searching. In some languages, it's possible to perform a runtime eval() that essentially...
1
by: Justin | last post by:
Is there any C# function that performs similarly to the Eval function in VB? Thanks in advance for your help!!
1
by: Matt Jensen | last post by:
Howdy I'm hoping to put a variable inside a variable name. In JavaScript, you would use eval(myvar+i) or something like that. What is the C# equiavlent of eval? Or how do I do it in C# .NET? ...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
3
by: matvdl | last post by:
I have been trying to get find where the simple Eval function that was such a great little function in asp has gone. There does not appear to be any replacement. Is this possible?? I was using...
4
by: jdc_1040 | last post by:
Could someone explain what the scope chain looks like to code running inside eval? (Please no "eval is the devil" essays :) ) Let's say I have a string of JavaScript that looks like this: var...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
13
by: Garg | last post by:
Hi, Could anybody please tell me how do I translate this line of code into vb.net?? I am having problem with this eval function. What is the equivalent of eval function in vb.net? str1=...
8
by: sonet | last post by:
Many Language have eval function.The C have eval function? How to execute c code in c ? Can i execute c code from a variable(like perl's code ref)? Or i must do this job by using yacc & lex ? All...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.