469,642 Members | 1,296 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Evaluating a mathematical string formula.

Hi guys,

I am building a string in code that is a mathematical formula, based on a
lot of criteria specified by the user. Once the formula is complete, I want
to evaluate it to get a result. For example:

dim strFormula as String

strFormula = "((2 + 2) * 3) / 4) - 5"

Once I have this formula, I want to evaluate it and get the result: -2.

Any tips on how I can do this?

Also, is there a way to check if the formula is valid (i.e. I didn't miss
any parenthesis or anything)? I guess I could try to evaluate it, and stick
it in a try - catch block.

Thanks for your time and help!

Wade
Nov 21 '05 #1
4 10328
Wade,

Only a link

By Peter Bromberg
http://www.eggheadcafe.com/articles/20030908.asp

I hope it helps

Cor
Nov 21 '05 #2
Three basic options:
1. Write your own postfix/prefix based expressions evaluator
2. Use Reflection.Emit to generate a function to evaluate this formula and
then execute that
3. Use the javascript eval function. There was a example posted in response
to a similar query a week or two back in this group on how to do the exact
same thing.

Rgds,
Anand M
VB.NET MVP
http://www.dotnetindia.com

"Wade" wrote:
Hi guys,

I am building a string in code that is a mathematical formula, based on a
lot of criteria specified by the user. Once the formula is complete, I want
to evaluate it to get a result. For example:

dim strFormula as String

strFormula = "((2 + 2) * 3) / 4) - 5"

Once I have this formula, I want to evaluate it and get the result: -2.

Any tips on how I can do this?

Also, is there a way to check if the formula is valid (i.e. I didn't miss
any parenthesis or anything)? I guess I could try to evaluate it, and stick
it in a try - catch block.

Thanks for your time and help!

Wade

Nov 21 '05 #3
The solution from the following website proved to be the perfect solution to
my problem:

http://www.codeproject.com/vb/net/ma..._evaluator.asp

Thanks!

"Anand[MVP]" <An******@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com...
Three basic options:
1. Write your own postfix/prefix based expressions evaluator
2. Use Reflection.Emit to generate a function to evaluate this formula and
then execute that
3. Use the javascript eval function. There was a example posted in response to a similar query a week or two back in this group on how to do the exact
same thing.

Rgds,
Anand M
VB.NET MVP
http://www.dotnetindia.com

"Wade" wrote:
Hi guys,

I am building a string in code that is a mathematical formula, based on a lot of criteria specified by the user. Once the formula is complete, I want to evaluate it to get a result. For example:

dim strFormula as String

strFormula = "((2 + 2) * 3) / 4) - 5"

Once I have this formula, I want to evaluate it and get the result: -2.

Any tips on how I can do this?

Also, is there a way to check if the formula is valid (i.e. I didn't miss any parenthesis or anything)? I guess I could try to evaluate it, and stick it in a try - catch block.

Thanks for your time and help!

Wade

Nov 21 '05 #4
"Wade" <wwegner23NOEMAILhotmail.com> schrieb:
I am building a string in code that is a mathematical formula, based on a
lot of criteria specified by the user. Once the formula is complete, I
want
to evaluate it to get a result. For example:

dim strFormula as String

strFormula = "((2 + 2) * 3) / 4) - 5"

Once I have this formula, I want to evaluate it and get the result: -2.


MathLib
<URL:http://www.palmbytes.de/content/dotnet/mathlib.htm>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dim | last post: by
1 post views Thread by kris Palmby | last post: by
6 posts views Thread by bugnthecode | last post: by
7 posts views Thread by david | last post: by
reply views Thread by humberto.bortolossi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.