Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Microsoft脚本控件评估';如果';表达式(通过c#)_C#_.net_Vbscript_Expression - Fatal编程技术网

使用Microsoft脚本控件评估';如果';表达式(通过c#)

使用Microsoft脚本控件评估';如果';表达式(通过c#),c#,.net,vbscript,expression,C#,.net,Vbscript,Expression,我有一些c#代码,它使用Microsoft脚本控件计算一些表达式: using MSScriptControl; // references msscript.ocx ScriptControlClass sc = new ScriptControlClass(); sc.Language = "VBScript"; sc.AllowUI = true; try { Console.WriteLine(sc.Eval(txtEx.Text).ToString()); } ca

我有一些c#代码,它使用Microsoft脚本控件计算一些表达式:

using MSScriptControl; // references msscript.ocx

ScriptControlClass sc = new ScriptControlClass();
sc.Language = "VBScript";
sc.AllowUI = true;

try
{
    Console.WriteLine(sc.Eval(txtEx.Text).ToString());
}
    catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
(txtEx是一个简单的文本字段)

数值表达式:“6+4”、“cos(34)”、“abs(-99)”、“round(1.234,2)”等都可以

布尔表达式:“真或假”、“1=2”都可以

但我如何评估一个简单的“如果”?我试过“if(true,2,3)”,“iif(true,2,3)”,“if(true)then 2 else 3”和“if(true)then 2 else 3 endif”

有人能帮我计算简单的条件表达式吗?非常感谢任何帮助


RH

< P>您应该考虑使用Windows工作流基础部分的表达式求值引擎。它的计算器和设计器都可以与WF分开使用。

尝试将IF表达式包装到函数中

Function test
   if (true) then
      return true
   else
      return false
   end if
End function
将函数添加到控件,然后使用Run

Result = ScriptControl.Run("Test")
(上面的代码没有经过测试,但这样做应该是可行的)

查看此链接了解更多信息


谢谢你的提示!在Brummo的帮助下,经过更多的实验,这似乎是可行的:

txtEx包含以下文本:

function test
  if (1=1) then 
    test = true
  else 
    test = false
  end if
end function
然后

sc.AddCode(txtEx.Text)


这并不理想,因为我真的想计算表达式,而不需要构建、加载和计算函数。(我很惊讶IIF不适用于简单的单行if评估)。

这个问题很老了。如果有帮助的话,你应该接受一个!!
function test
  if (1=1) then 
    test = true
  else 
    test = false
  end if
end function
object[] myParams = { };
Console.WriteLine(sc.Run("Test", ref myParams).ToString());
using MSScriptControl; // references msscript.ocx

ScriptControlClass sc = new ScriptControlClass();
sc.Language = "VBScript";
sc.AllowUI = true;

// VBScript engine doesn’t have IIf function
// Adding wraper IIF function to script control.
// This IIF function will work just as VB6 IIf function.
sc.AddCode(@"Function IIF(Expression,TruePart,FalsePart)
                If Expression Then
                    IIF=TruePart
                Else
                    IIF=FalsePart
                End IF
            End Function");
try
{
    //define x,y variable with value
    sc.AddCode(@"x=5
                y=6");
    //test our IIF 
    Console.WriteLine(sc.Eval("IIF(x>y,x,y)").ToString());
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}