I can Store an expression within a Variable?

  • Thread starter Thread starter Daniel R. Rossnagel
  • Start date Start date
D

Daniel R. Rossnagel

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 "
 
Daniel said:
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.
 
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
 
Daniel said:
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.
 
\\\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
 
Gabriel said:
\\\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");
 
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.
 
Hi James,

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

Thanks,
Bart
 
Back
Top