Click here to Skip to main content
15,891,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was creating Form for calculating weight of item


I was stored formula to database in string format.

While calculating i was take that formula to label in my form and i am taking value from user.

the Formula is "(val1-val2)*val2*0.00314*val3"

after taking user input i was replace the value of val1, val2 & val3 with user input

so basically my formula is like "(51-5)*5*0.00314*4" in this 51,5,4 is user inputs

and i am store this formula to label

now i want to use that formula for calculating weight, so i am convert label value to double.

But it shows me error "input string is not in correct format."

Can anyone help me.

What I have tried:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim bindformula As String = lblActualFormula.Text
       bindformula = bindformula.Replace("Val1", txtOd.Text)
       bindformula = bindformula.Replace("Val2", txtWT.Text)
       bindformula = bindformula.Replace("Val3", txtLength.Text)
       lblBindFormula.Text = bindformula
   End Sub


   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
       Dim val As Double = Double.Parse(lblBindFormula.Text)
       MsgBox(val.ToString())
   End Sub
Posted
Updated 7-May-18 2:27am

You can't just try to parse an expression to a value - it doesn't work like that.
You need to evaluate it properly instead - tehre are several evaluators on this site: A Tiny Expression Evaluator[^] is a good one, and you can add its assembly as a reference to your project and call it direct from VB.
 
Share this answer
 
Comments
Maciej Los 7-May-18 8:27am    
5ed!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900