views:

143

answers:

5

Is it possible to assign an int variable a value that is a result of expression written in a string? E.g. I have a string "5 - 3" and the expected result is 2.

+2  A: 

You can use Calculator.NET

Giorgi
Thx for this. Will be useful at some point.
Noel Abrahams
Thanks! I believe that is what I need.
tyumener
A: 

There is nothing built into the framework, so you can either built your own mathematical expression parser, or use one of the many libraries out there.

A search on ".net arithmetic parser" comes up with many results, some free, some not.

For instance:

Oded
A: 

Another open one: http://www.c-sharpcorner.com/uploadfile/mgold/codedomcalculator08082005003253am/codedomcalculator.aspx

Ruel
Thanks for your reply. That's what I need. Unfortunately can not mark it as useful - don't have enough reputation.
tyumener
That's no problem. :)
Ruel
A: 

http://www.codeproject.com/KB/dotnet/Expr.aspx

manixrock
That's the arcticle I start my search with :)
tyumener
+1  A: 

If you fancy writing your own (I did, it's fun!). Have a look on Wikipedia for:

Shunting Yard Algorithm and Reverse Polish Notation

The pseudo-code is really clear and it's a great learning experience

RichK