Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one string in which I have written some formulas for calculation. Eg.

Dim strFormula(3) as string 
strFormula(0) = "(txtBasic.Text) * 2"
strFormula(1) = "((txtBasic.Text) + (txtBasic.Text)) * 2"
strformula(2) = "((txtBasic.Text) * 10) /2)"


Now by using this formula I have to get the result from textbox name "txtBasic".

Is there any way that I can directly convert string into formula or value?
Posted
Updated 22-Aug-10 22:35pm
v2
Comments
GPUToaster™ 23-Aug-10 2:15am    
Why not use Integer.Parse or double.parse??
Dalek Dave 23-Aug-10 4:34am    
Minor Edit for Grammar.

1 solution

Why not just use a Select Case like this :
VB.NET
Dim intToCalculate As Integer = 0
        Integer.TryParse(txtBasic.Text, intToCalculate)
        Select Case cboSelect.SelectedIndex 'use appropriate selector here
            Case 0
                txtResult.Text = intToCalculate * 2
            Case 1
                txtResult.Text = (intToCalculate * 2) * 2
            Case 2
                txtResult.Text = (intToCalculate * 10) / 2
        End Select

If you need to use it in more than one place then create a function like this:
VB
Enum FormulaType
       Formula1
       Formula2
       Formula3
   End Enum

   Private Function FormulaResult(ByVal intToCalculate As Integer, ByVal yourFormulaType As FormulaType) As Integer

       Dim intReturn As Integer = 0
       Select Case yourFormulaType
           Case FormulaType.Formula1
               intReturn = intToCalculate * 2
           Case FormulaType.Formula2
               intReturn = (intToCalculate * 2) * 2
           Case FormulaType.Formula3
               intReturn = (intToCalculate * 10) / 2
       End Select
       Return intReturn

   End Function

Hope this helps.
Happy Coding :)
 
Share this answer
 
Comments
Dalek Dave 23-Aug-10 4:34am    
Hey, Good Answer!
Wayne Gaylard 23-Aug-10 4:47am    
Thanks!

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