将文本公式转换为值 [英] Convert Text formula into value
本文介绍了将文本公式转换为值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,其中写了一些计算公式.例如
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)"
现在,通过使用此公式,我必须从文本框名称"txtBasic"中获取结果.
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?
推荐答案
为什么不只使用像这样的Select Case:
Why not just use a Select Case like this :
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:
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 :)
这篇关于将文本公式转换为值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文