VBA Eval函数在C#中的使用方式:

huangapple go评论69阅读模式
英文:

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(&quot;4 &gt; 2 AND 10 &lt;&gt; 62&quot;) &#39;Would return true
evalResult = Application.Eval(&quot;&#39;True&#39; = &#39;False&#39; AND (4 = 2 OR 4 = 4)&quot;) &#39;Would return false
evalResult = Application.Eval(&quot;&#39;True&#39; = &#39;True&#39; AND (4 = 2 OR 4 = 4)&quot;) &#39;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.

huangapple
  • 本文由 发表于 2023年7月27日 20:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定