eval possible?

  • Thread starter Thread starter RP
  • Start date Start date
R

RP

Hi all, I was wondering if it is possible to execute this code and if so,
how to go about doing it?

Dim val1, val2 as integer
val1 = 10
val2 = 15

Dim str as string = "avg(val1, val2)"

eval (str) ??

function avg(val1 as integer, val2 as integer) as integer
return ((val1+val2)/2)
end function

TIA,
RP
 
You can do this with the MS Script control. Otherwise you'll have to use
reflection and build an assembly on the fly. Or, use JScript.NET, which has
a built-in Eval() function.
 
Hi GP,

Here's some code:

Dim oCalculator = New ExpressionEvaluator
oCalculator.AddVar ("val1", 10)
oCalculator.AddVar ("val2", 15)
Dim dResult = oCalculator.Calc ("Avg (val1, val2)")

Is this the kind of thing that you are after?

Does Avg have to be one of <your> functions?
Then it might have to be:

oCalculator.AddFunc ("Avg", Me.Avg, 2) '2 args
Dim dResult = oCalculator.Calc ("Avg (val1, val2)")

Regards,
Fergus
 
You can do this with the MS Script control. Otherwise you'll have to use
reflection and build an assembly on the fly. Or, use JScript.NET, which has
a built-in Eval() function.

Or create a dll with JScript.NET that calls Eval and then reference it
from your VB.NET project :)
 
* "RP said:
Hi all, I was wondering if it is possible to execute this code and if so,
how to go about doing it?

Dim val1, val2 as integer
val1 = 10
val2 = 15

Dim str as string = "avg(val1, val2)"

eval (str) ??

function avg(val1 as integer, val2 as integer) as integer
return ((val1+val2)/2)
end function

Samples:

<http://www.codeproject.com/useritems/evaluator.asp>
<http://www.codeproject.com/csharp/livecodedotnet.asp>

If you have a DevX account:

<http://www.devx.com/codemag/Article/10352/0/page/1>

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
 
Tom, where can I find some sample code in Jscript.net. I have never used it
before. Thanks so very much.
 
Back
Top