Quantcast
Channel: NCalc - Mathematical Expressions Evaluator for .NET
Viewing all articles
Browse latest Browse all 58

Created Unassigned: nCalcExpression.EvaluateFunction += removes support of the pre-existing if() function [28356]

$
0
0
nCalcExpression.EvaluateFunction += removes support of the pre-existing if() function

For example:

var nCalcExpression = GetNCalcExpression("if(1<2,'true','false')");
nCalcExpression.EvaluateFunction += NCalcExtensions.NCalcExtensionFunctions;
var resultObject = nCalcExpression.Evaluate();
var resultString = resultObject.ToString();

This will result in an exception, commenting out the second line would not.

The workaround is to add the following to your static NCalcExtensions.NCalcExtensionFunctions method:

public static void NCalcExtensionFunctions(string functionName, FunctionArgs functionArgs)
{
string param1;
string param2;
switch (functionName)
{
case "if":
bool boolParam1;
object objectParam1;
object objectParam2;
try
{
boolParam1 = (bool)functionArgs.Parameters[0].Evaluate();
objectParam1 = functionArgs.Parameters[1].Evaluate();
objectParam2 = functionArgs.Parameters[2].Evaluate();
}
catch (Exception)
{
throw new MacroParameterException(FunctionConst, "if() requires three parameters.");
}
functionArgs.Result = boolParam1 ? objectParam1 : objectParam2;
return;

... other cases

}
}



Viewing all articles
Browse latest Browse all 58

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>