Calculate formula from string?

  • Thread starter Thread starter Klaus Jensen
  • Start date Start date
K

Klaus Jensen

Hi!

I am currently converting an ASP/SQL Server application to VB.Net/Access.
The application performs calculations based on which products are selected.
Therefore i need to be able to store formulas in the database, replace
placeholdes with values and execute the calculation.

Earlier this was done by executing the formula on SQL Server and retrieving
the result, but that is no longer possible - or desireable for that matter.

So if I have a string that says "2*2" how to I execute this and get the
result back?

(which my sharp brain tells me should be somewhere around 4) ;)

Thanks in advance

- Klaus
 
I don't know ASP.Net but if it supports VB Script then there is a function
called 'Eval' ( and also Execute) which does exactly what you are looking
for.
 
Hi Klaus,

|| I have a string that says "2*2" ...
|| (which my sharp brain tells me should be somewhere around 4) ;)

You know, I think you're right! Have you ever thought of going into
programming? I think you'd be good at it. ;-)

Here's one implementation of an arithmetic evaluator class.
http://www.palmbytes.de/content/dotnetlibs/mathlib.htm

The site is in German but the Download link is obvious and everything's in
English in
the zip file.The app is about showing a graph based on user-defined equations.
Have a dig around inside the evaluator. You'll probably have to do a bit of
work getting your calculator variables set up, ditto additional functions.
Then you're off!

If you want full-on calculation - anything that VB.NET can calculate - you
might like to explore the VSA Namespace. This is the realm of the scripting
engine and can handle some powerful code. Naturally it's a lot more effort to
get going. There is a newsgroup just for this topic:
microsoft.public.dotnet.vsa, though it's not hugely busy.

Another option you could explore is dynamic compilation. I haven't looked
at that myself, so my paragraph runs out here.

Out of interest, could you post some examples of the expressions that
you'd like to evaluate?

Have fun. ;-)

Regards,
Fergus
 
Fergus Cooney said:
If you want full-on calculation - anything that VB.NET can calculate - you
might like to explore the VSA Namespace. This is the realm of the scripting
engine and can handle some powerful code. Naturally it's a lot more effort to
get going. There is a newsgroup just for this topic:
microsoft.public.dotnet.vsa, though it's not hugely busy.

This seems to be the best bet, also because this would possibly be a
valuable .net lesson I might be able to use later. :)
Out of interest, could you post some examples of the expressions that
you'd like to evaluate?

Well, okay then.

5,0722611E-13*x*x*x*x - 6,82931882931E-09*x*x*x +
0,0000343139083138486*x*x - 0,0762394457392556*x + 74,1895493393322

Hope your're a happy trooper now! :D

Thanks for your help. :)

- Klaus
 
Hi Klaus,

Thanks for the expression. I'm trooping off to calculate it right now,
lol.

I'm interested in expression evaluators and am currently converting an old
one that I wrote for Dos. It had functions like File("C:\Foo.txt").Line(3) =
"Whatever" and RunProg ("Dir *.*").Contains (" 0 Files").

I was just wondering whether you had any functions or anything that might
give me additional food for thought.

Regards,
Fergus
 
Back
Top