Wednesday, May 09, 2007

How to calculate value with regular expression using C#.NET 2,0 Ex: (A+B) *C

How to calculate value with regular expression using C#.NET 2,0

Some time it require to calculate value with expression
For example if i write (A+B)*C in the textbox, i want some function calculate this value with formula.

For this purpose you can use two DLLs 1) Microsoft.JScript.dll and 2) Microsoft.Vsa.dll
This is available in (Visual Studio 2005 DOTNET version 2.0)

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ folder.

Namespace
using Microsoft.JScript;
using Microsoft.JScript.Vsa;
using Microsoft.Vsa;

Variable
private
static VsaEngine engine = VsaEngine.CreateEngine();

Source Code
public static object Eval(string Expression)
{
try
{
return Microsoft.JScript.Eval.JScriptEvaluate(Expression, engine);
}
catch (Exception x)
{
return "ERROR! " + Expression + x.Message;
}
}

How to Call this function

string val = "10*20+(20-8)"
Eval(val); // this will give result --> 212

val = "(10*20)+(20-8)*9-(90/40)"
Eval(val); // this will give result --> 305.75

13 comments:

Kelvin Watson said...

Hi There,

My name is Kelvin, Watson and I work for www.freeonlinegames.com I came across your website and I am very impressed, Moreover, I was wondering if you can add our link on your website. We are one the top 10 free gaming website out there. We offer free downloads of games and I guarantee you that visitors on your website will appreciate seeing our link on your website. We thank you, for your consideration.

If you do decide to add our link, please add the following description if you like

Play free games online. Thousands of games to choose from. Racing games, shooting games, flying games, puzzle games, RPG games and many more!

If you have any questions or comments, please feel free to contact me at cs@freeonlinegames.com

www.freeonlinegames.com

Kind Regards,

Kelvin Watson

FreeOnlineGames.com

Ritesh_Kesharwani said...

ok no problem sir
i will include your link with my next artical onward

thanks

சம்பத் said...

Thanks ritesh for the article which u have published which I had needed as the case in my scenario. But I needed to calculate the expression in terms of dynamic variables which the users might change

ex. grosssalary+HRA-Basicpay

I need to calculate this expression into a value. When i declare the varaibles and assign values to it. Its not evaluating the expression. Itz saying above variables are not declared. Please give a solution for it...

Rohit_India said...

My name is Rohit Kumar. I came across your website and I am very impressed.
Thanks ritesh for the article which u have published

avi said...

Hi Ritesh,
I am using Visual studio2005 with .NET framework 3.0....
For in that ur code is giving error for namespace used.
Kindly , tell me changes I have to do in my code

Ratnesh Bharos said...

Great man your post is really help me. I want to say thanks to you.

Anonymous said...

Bonjour, riteshk.blogspot.com!
[url=http://cialispite.pun.pl/ ] cialis en ligne[/url] [url=http://viagravalo.pun.pl/ ]Achat viagra en ligne[/url] [url=http://cialisanta.pun.pl/ ]Acheter cialis [/url] [url=http://viagrapeck.pun.pl/ ]Achat viagra en ligne[/url] [url=http://cialisycin.pun.pl/ ]Acheter du cialis [/url] [url=http://viagrappro.pun.pl/ ] viagra en ligne[/url]

Anonymous said...

Good day !.
might , perhaps very interested to know how one can manage to receive high yields .
There is no need to invest much at first. You may begin to get income with as small sum of money as 20-100 dollars.

AimTrust is what you thought of all the time
AimTrust incorporates an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.

It is based in Panama with structures around the world.
Do you want to become an affluent person?
That`s your choice That`s what you wish in the long run!

I feel good, I started to take up income with the help of this company,
and I invite you to do the same. If it gets down to select a correct companion utilizes your funds in a right way - that`s AimTrust!.
I take now up to 2G every day, and my first investment was 500 dollars only!
It`s easy to start , just click this link http://kawyvutot.freecities.com/nahipy.html
and go! Let`s take our chance together to become rich

Anonymous said...

Hello!
You may probably be very interested to know how one can manage to receive high yields on investments.
There is no initial capital needed.
You may commense earning with a money that usually is spent
on daily food, that's 20-100 dollars.
I have been participating in one company's work for several years,
and I'll be glad to let you know my secrets at my blog.

Please visit my pages and send me private message to get the info.

P.S. I make 1000-2000 per daily now.

[url=http://theblogmoney.com] Online investment blog[/url]

Anonymous said...

Hi!
You may probably be very curious to know how one can make real money on investments.
There is no need to invest much at first.
You may begin earning with a sum that usually is spent
on daily food, that's 20-100 dollars.
I have been participating in one project for several years,
and I'm ready to share my secrets at my blog.

Please visit my pages and send me private message to get the info.

P.S. I earn 1000-2000 per day now.

http://theinvestblog.com [url=http://theinvestblog.com]Online Investment Blog[/url]

Anonymous said...

Amiable post and this post helped me alot in my college assignement. Gratefulness you seeking your information.

Anonymous said...

Hi everybody!

For sure you didn’t here about me yet,
friends call me Peter.
Generally I’m a social gmabler. recently I take a great interest in online-casino and poker.
Not long time ago I started my own blog, where I describe my virtual adventures.
Probably, it will be interesting for you to utilize special software facilitating winnings .
Please visit my blog. http://allbestcasino.com I’ll be interested on your opinion..

Anonymous said...

i very much love all your posting taste, very helpful,
don't give up and also keep posting mainly because it simply well worth to read it.
looking forward to look into even more of your own articles, have a great day!