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
110
CalcSettings throws null reference error
posted

Hello,

I am trying to calculate some values on realtime using CalcManager.

However I am getting a null reference exception inside calcSetting.Formula result.

var textBox1 = new TextBox();
var textBox2 = new TextBox();
textBox2.Text = "15";
sectionElement.ControlName = "CustomFormula";
sectionElement.Formula = expression;
sectionElement.ColumnName = columnName;
PropertyNode.Tag = sectionElement;
CalcSettings calcSettings = new CalcSettings();
calcSettings.Formula = "2";
calcSettings.PropertyName = "Text";
UltraCalcManager calcManager = new UltraCalcManager();
calcManager.SetCalcSettings(textBox1, calcSettings);
var a = textBox1.Text;

Any idea?

Parents
No Data
Reply
  • 469350
    Offline posted

    Hi,

    I'm not sure exactly what you are trying to achieve here, but my guess is that you are probably not going about it the best way. The code you posted here includes a bunch of objects which are not defined, like sectionElement, expression, and PropertyNode. In addition to that, there are several problems here.

    First... in order for any control to be used as a formula source or a formula target, it has to exist in the calculation network. You are including textBox1 in the calc network when you call SetCalcSettings. So that's good.

    But textBox1 is created at run-time and it's never assigned a name. So it's Name property will return an empty string and that's a problem. So you need to assign it a name:

    textBox1.Name = "textBox1";

    Another issue is that you are called SetCalcSettings and then immediately trying to access the Text property of the TextBox. But the Calculations are performed asynchronously, so nothing will be calculated at this point. So for this to work you would have to either wait for the calculation to complete, which you could do by handling the CalculationsCompleted event. Or you would have to set the CalcFrequency to Synchornous. So something like this would work:

                var textBox1 = new TextBox();
                textBox1.Name = "textBox1";
                CalcSettings calcSettings = new CalcSettings();
                calcSettings.Formula = "2";
                calcSettings.PropertyName = "Text";
                UltraCalcManager calcManager = new UltraCalcManager();
                calcManager.CalcFrequency = CalcFrequency.Synchronous;
                calcManager.SetCalcSettings(textBox1, calcSettings);
                var a = textBox1.Text;

    Of course, if all you want to do is calculate the result of a simple formula, then this is far more difficult and convoluted than you need. You are not even adding the TextBox to the form, so there's really no reason to create one. It would be simpler to use a NamedReference.

                UltraCalcManager calcManager = new UltraCalcManager();
                var myNamedReference = calcManager.NamedReferences.Add("MyNamedReference", "2");
                calcManager.CalcFrequency = CalcFrequency.Synchronous;
                var a = myNamedReference.FormulaResult.Value;

    Or, if you don't need to re-use and maintain the value and this is just a one-shot calculation, you could just call the Calculate method:

                UltraCalcManager calcManager = new UltraCalcManager();
                var a = calcManager.Calculate("2 + 2");

    But CalcManager is intended for use with UI controls. So even that last method call to the Calculate method is a lot less efficient than just doing the calculation in c# code. The Calculate method puts the calculation into the calc network and so it will get calculated based on the calc network's sort order. If you have a bunch of other controls with formulas, it could take a while for the CalcManager to get to that calculation. So it would be far more efficient to do this:

    var a = 2 + 2;

    So what you do here and how you do it depends a lot on what you are trying to achieve.

Children
No Data