• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

VB 2008: Calculate mathematical string from textbox

I have a form with a textbox. Users can enter any mathematical string.

How do I get vb to calculate any mathematical string and spit it out in a msgbox.

i.e.1. if the user enters 23+34/2, a messagebox will popup stating: "40":
i.e.2. if the user enters 1+5/5-6+2-1 a messagebox will popup stating: "-3"

Visual Basic 2008 does not seem to have a "EVAL()" function.


0
ouestque
Asked:
ouestque
  • 2
3 Solutions
 
Jorge PaulinoIT Pro/DeveloperCommented:
Check this example of an expression evaluator
http://www.codeproject.com/KB/vb/expression_evaluator.aspx 
0
 
game-masterCommented:

GOOD AFTERNOON

Click on Project --> Add Reference.
Switch to the COM tab.
Select the "Microsoft Script Control 1.0" entry and click OK.

Then use code like this:

        Dim ScriptEngine As New MSScriptControl.ScriptControlClass
        ScriptEngine.Language = "VBScript"
        Dim problem As String = "(150 + 20) * 2 / (7 - 4)"
        Dim answer As String = ScriptEngine.Eval(problem)
        MessageBox.Show(answer)


I HOPE I COULD GIVE U IDEA..


game-master
0
 
game-masterCommented:


change the "MSScriptControl.ScriptControlClass" to "MSScriptControl.ScriptControl"

it works fine...

game-master
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now