Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1075
Using null values in NamedReferences
posted

Can someone tell me how I can use null values in named references with an UltraCalcManager? I've created a formula editor which uses data from our database (using named references). However, sometimes the data contains null values. In that case, I want to assign a default value in the formula. But I can't seem to get it working. Below is some example code.

const string formula = "IF( ISNULL( [test] ), 100, [test] )";

var ultraCalcManager1 = new UltraCalcManager();
ultraCalcManager1.NamedReferences.Add("test", "75");

var result = ultraCalcManager1.Calculate(formula);

ultraCalcManager1.NamedReferences.Clear();
ultraCalcManager1.NamedReferences.Add("test", "null()");

result = ultraCalcManager1.Calculate(formula);

The first result is fine, but with the second result I get an error. I tried all sorts of different values for the null namedreference, like null without parentheses, or even without quotes. How can I accomplish this?

Thanks in advance!

Parents
No Data
Reply
  • 469350
    Verified Answer
    Offline posted

    I tried this out and I am getting the same results. But the results I get are very strange. 

    If I tried this in the Form_Load event, I get the same results as you.

    If I create an UltraCalcManager and place it on the form and then use a TextBox control for the formula result, it works fine.

    Using a CalcManager that is on the form with the Calculate method does not work inside the Form_Load, but it works later on if I use a button_click event.

    So I think the problem is that the code you have here is trying to perform operations on the CalcManager synchonrously. You are expecting that when you add a new NamedReference to the NamedReferences collection that the NamedReference is immediately available for use in a formula. But this is not the case, by default. It works okay when your formula is '75' because that's a literal value and requires no extra calculation. But "null()" is a function. I also tried the same thing using "sum(5, 5)" and got the same results, so it's not just null that is a problem, it's any function. 

    Anyway, if I am right, then the solution is simply this:

                const string formula = "IF( ISNULL( [test] ), 100, [test] )";

                var ultraCalcManager1 = new UltraCalcManager();
                ultraCalcManager1.CalcFrequency = CalcFrequency.Synchronous;

                ultraCalcManager1.NamedReferences.Add("test", "75");

                var result = ultraCalcManager1.Calculate(formula);

                ultraCalcManager1.NamedReferences.Clear();
                ultraCalcManager1.NamedReferences.Add("test", "null()");

                result = ultraCalcManager1.Calculate(formula);    

    I tried this out and it works fine for me.

Children