Evaluating the Expression in VB.net
-
Tuesday, June 26, 2012 5:27 AM
Hi...
I need to evaluate the expression which is given in TextBox. The expression can be any math function or simple arithmetic expression. I am sure that there will be some function to do this directly because in older development software it is available.
Please help..
Thanks and Regards..
Shrinidhi Acharya
- Moved by Mike Dos ZhangMicrosoft Contingent Staff, Moderator Wednesday, June 27, 2012 5:21 AM it should be a common programming question, rather than a Windows Desktop dev question (From:General Windows Desktop Development Issues)
All Replies
-
Wednesday, June 27, 2012 5:56 AMModerator
Hi shrinidhi,
Thanks for you post.
As for you issue, I’d like to do some clarify.
>>because in older development software it is available.
Would you like to share more information about this? Do some math methods are available in old versions and not available or caused some exception at now?
It will be better if you can share the code about the expression.
I look forward your reply.
Mark Liu-lxf [MSFT]
MSDN Community Support | Feedback to us
- Edited by Mark Liu-lxfModerator Wednesday, June 27, 2012 5:56 AM
- Edited by Mark Liu-lxfModerator Wednesday, June 27, 2012 5:56 AM
-
Wednesday, June 27, 2012 6:26 AM
There is some discussion here:
http://social.msdn.microsoft.com/Forums/en/vblanguage/thread/41466b93-56cb-4dd0-8a16-d53aec3c4a2fThere is a complete project here:
http://www.codeproject.com/Articles/9519/An-expression-evaluator-written-in-VB-NETand some more here:
http://www.freevbcode.com/ShowCode.asp?ID=1263- Proposed As Answer by John Anthony Oliver Thursday, June 28, 2012 2:23 PM
- Marked As Answer by Mark Liu-lxfModerator Friday, July 06, 2012 7:43 AM
-
Thursday, June 28, 2012 2:22 PM
Hi,
For a simple mathematical expression try the .Compute()
method for a DataTable.
'
Add one Button and one TextBox to your Form to try this code please.>>
'
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TextBox1.Text = "2*3+8" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dt As New DataTable Dim result As Decimal Try result = Convert.ToDecimal(dt.Compute(TextBox1.Text, "")) MessageBox.Show(result.ToString, "Result = ") Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub End Class
Regards,
Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
- Proposed As Answer by .paul. _ Thursday, June 28, 2012 4:39 PM
- Marked As Answer by Mark Liu-lxfModerator Friday, July 06, 2012 7:43 AM
-
Thursday, June 28, 2012 4:44 PMModerator
The datatable or script host solutions are probably the most efficient.
A CodeDom solution might be more versitle but will incur more overhead. A full parser/lexer that supports all possible expression syntax could be a lot of work, could be slow if it is not implemented properly, and would be overkill for simple expressions.
If you expect all of the expressions to be simple, use the datatable as suggested by John Oliver. For more complex expressions, the script host is probably the way to go.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
-
Thursday, June 28, 2012 4:45 PMModeratordangit .paul._ I wondered why kept getting an error trying to propose this post! You snuck in and beat me to it. =)
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
-
Monday, July 02, 2012 5:15 AM
Your solution works superfine with arithmetic expression but if i need to insert mathematical expressions like sin(), cos() etc.. it does not work..
Shrinidhi Acharya
-
Monday, July 02, 2012 6:25 AM
Your solution works superfine with arithmetic expression but if i need to insert mathematical expressions like sin(), cos() etc.. it does not work.
If you want to use those types of functions you will need to look at some of the other options that have been suggested. Each evaluator will support a different set of functionality.

