英文:
VBA Eval Function in C#
问题
我试图在C#中重写一些VBA代码。VBA函数接受一个类似以下的字符串:
"4 > 2 AND 10 <> 62" 或
"'False' = 'False' AND 6 <> 7" 或
"'True' = 'False' AND (4 = 2 OR 4 = 4)"
VBA中有一个名为"Application.Eval()"的内置函数,可以评估这些字符串并返回true或false。文本比较没有问题。
Dim evalResult as Boolean
evalResult = Application.Eval("4 > 2 AND 10 <> 62") '会返回true
evalResult = Application.Eval("'True' = 'False' AND (4 = 2 OR 4 = 4)") '会返回false
evalResult = Application.Eval("'True' = 'True' AND (4 = 2 OR 4 = 4)") '会返回True
现在,我尝试在C#中使用NCalc来执行相同的操作。但是,NCalc似乎只处理数值操作,我没有找到类似的C#内置函数。
是否在C#中有类似于VBA中的Application.Eval()函数的内置函数或类似的东西?
英文:
im trying to rewrite some VBA code in C#. <br>
The VBA function takes a string like <br> <br>
"4 > 2 AND 10 <> 62" or <br>
"'False' = 'False' AND 6 <> 7" or <br>
"'True' = 'False' AND (4 = 2 OR 4 = 4)". <br>
(Without the "" marks) <br> <br>
VBA has an implemented function called "Application.Eval()". <br>
This function can evaluate these strings and return true or false. Text comparison is no
Problem. <br>
DIM evalResult as Boolean
evalResult = Application.Eval("4 > 2 AND 10 <> 62") 'Would return true
evalResult = Application.Eval("'True' = 'False' AND (4 = 2 OR 4 = 4)") 'Would return false
evalResult = Application.Eval("'True' = 'True' AND (4 = 2 OR 4 = 4)") 'Would return True
Now i tried to to the same in c# and evaluate these strings using NCalc. <br>
But NCalc seems to only handle numerical operations and i did not find any mentioning of a similar function in c#. <br>
Is there a built in function or something similar in C# to the Application.Eval() function in VBA?
答案1
得分: 0
如果我将字符串中的 "AND" 替换为 "&",将 "OR" 替换为 "|",它可以正常工作并返回正确的布尔值。
英文:
Okay, nvm.
If i replace "AND" with "&" and "OR" with "|" in the string, it works and returns the right bool.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论