469,898 Members | 1,599 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

I can Store an expression within a Variable?

Sample:

String c="1+2+3"

since it can makes to obtain the value of this Expression?

Other Sample:

Int X=4
String c="1+2+3+X "
Nov 24 '05 #1
8 2278

Daniel R. Rossnagel wrote:
Sample:

String c="1+2+3"

For your first example, I believe that VB.NET contains some kind of
expression evaluator method, although I'm not sure about that. You
can, of course, write your own suite of expression parsers and
evaluators. It's so easy that I'm sure that there are several out on
the Web if you search for them.
Other Sample:

Int X=4
String c="1+2+3+X "


This, however, would not be possible as written, because at run time
the variable you called "X" no longer has a name, and may even have
been optimized away, so there would be no way to find "X" and discover
what its value was. There are other ways of accomplishing the same
thing, but you can't just mention the name of a variable somewhere and
trust that you can find it using Reflection or anything like that.

Nov 24 '05 #2
Thanks. It happens that I come from VFP and it used much the
macrosubstitution

Sample:
X=4
C="1+2+3+X "
R=&C

Strange much this of the VFP

"Bruce Wood" <br*******@canada.com> escribió en el mensaje
news:11**********************@g44g2000cwa.googlegr oups.com...

Daniel R. Rossnagel wrote:
Sample:

String c="1+2+3"


For your first example, I believe that VB.NET contains some kind of
expression evaluator method, although I'm not sure about that. You
can, of course, write your own suite of expression parsers and
evaluators. It's so easy that I'm sure that there are several out on
the Web if you search for them.
Other Sample:

Int X=4
String c="1+2+3+X "


This, however, would not be possible as written, because at run time
the variable you called "X" no longer has a name, and may even have
been optimized away, so there would be no way to find "X" and discover
what its value was. There are other ways of accomplishing the same
thing, but you can't just mention the name of a variable somewhere and
trust that you can find it using Reflection or anything like that.

Nov 24 '05 #3

Daniel R. Rossnagel wrote:
Thanks. It happens that I come from VFP and it used much the
macrosubstitution

Sample:
X=4
C="1+2+3+X "
R=&C


I'm not familiar with VFP, but what you say is possible in an
interpreted language, in which symbolic information is always available
at runtime.

In compiled languages this isn't (necessarily) true.

Nov 24 '05 #4
\\\Eval by Nigel Amstrong
1. Create a file called: DynamicMath.js
2. Add this code to it:

class DynamicMath
{
static function Eval(MathExpression : String) : double
{
return eval(MathExpression);
};
}

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.JScript.dll as well)
5. Use from your favourite .NET language:
Dim d As Double = DynamicMath.Eval("2 + 3 + 4")
MessageBox.Show(d)
6. That's it
Nov 24 '05 #5

Gabriel Lozano-Morán wrote:
\\\Eval by Nigel Amstrong
1. Create a file called: DynamicMath.js
2. Add this code to it:

class DynamicMath
{
static function Eval(MathExpression : String) : double
{
return eval(MathExpression);
};
}

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.JScript.dll as well)
5. Use from your favourite .NET language:
Dim d As Double = DynamicMath.Eval("2 + 3 + 4")
MessageBox.Show(d)
6. That's it


Very cool.

However, one still couldn't do one of the things the OP wants to do,
which is:

Dim x As Double = 1.25;
Dim d As Double = DynamicMath.Eval("2 + 3 + x");

Nov 25 '05 #6
Hi Bruce,

What about Eval("2+3" + x) ?

succes,
Bart

http://www.xenopz.com/blog/bartdeboeck/

Nov 25 '05 #7
ba**********@hotmail.com wrote:
What about Eval("2+3" + x) ?


That's not even going to compile. I think you meant:

Eval("2 + 3") + x;

Which is doable if you know in advance that the only thing you're going to
do with X is add it to the expression.

--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 25 '05 #8
Hi James,

It will compile in C#, the expression, however, should be Eval("2+3+"
+ x)

Thanks,
Bart

Nov 29 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Sanjay | last post: by
8 posts views Thread by No Such Luck | last post: by
1 post views Thread by TARUN | last post: by
25 posts views Thread by Mike | last post: by
18 posts views Thread by dspfun | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.