Monday, May 24, 2010

Strings in C#?

if a user enters the expression in a textbox e.g 3 + 5


what is the code in c# that will take the string and evaluate


the expression?

Strings in C#?
See the code here:


http://www.codeproject.com/KB/cs/evalcsc...





It shows how to implement an "eval" function that inputs a string and evaluates it as code. If it's added to your project with the imports, you could do something like:





MessageBox.Show(eval("3+4;").toString(...





If all you need to do is add numbers though, it's overkill and it would be better to scan through the string for the "+" and extract the numbers on opposite sides.
Reply:Tryparse(textBox1-%26gt;Text,num1);


Tryparse(textBox2-%26gt;Text;num2);


num3=num1*num2;





textBox3-%26gt;Text=num3.toString();


or


MessageBox(num3.toString());


No comments:

Post a Comment