Add operator for custom objects

Posted by: anna-fp on 8 September 2017, 2:06 pm EST

  • Posted 8 September 2017, 2:06 pm EST

    Hi,

    I have a custom class with overloaded "+" operator:

    public class MyItem
        {
            public int Value { get; set; }
            public static MyItem operator +(MyItem left, MyItem right) { return new MyItem{Value = left.Value + right.Value };}
            public override string ToString() { return Value.ToString(); }
        }

    Collection of MyItem's objects is data for spreadsheet.

                var item1 = new MyItem {Value = 1};
                var item2 = new MyItem { Value = 2 };

                var model = new DefaultSheetDataModel(2, 2);
                model.SetValue(0, 0, item1); // see "1"
                model.SetValue(0, 1, item2); // see "2"

                fpSpread1_Sheet1.Models.Data = model;
                fpSpread1_Sheet1.SetActiveCell(1,1);

    If you run this code, you see grid where A1= 1 and B1=2.

    Add the following code:

                MyItem result = item1 + item2;
                model.SetValue(1, 0, result);// see "3"

                model.SetFormula(1, 1, "A1+B1"); // see #VALUE! , but A1+B1 = item1 + item2 = result

    How can I use standard operations/functions for custom objects?

    "Add" operator doesn't work for strings too!!!

    Anna.

  • Replied 8 September 2017, 2:06 pm EST

    Anna,


    The spreadsheet is working as intended.  The spreadsheet's built-in Excel-like operators and functions work with Excel-like data types (i.e. number, string, boolean, and error values).  The built-in operators and functions do not know about custom data types and thus do not work with custom data types.  The custom data types and their operators are defined in your C# application code but are not defined in the spreadsheet.  When an unrecognized data type is encountered, the formula will evaluate to a #VALUE! error.  You would need to create your own custom functions to work with your custom data types.  This would involve deriving a custom function class from the FunctionInfo class and then calling the AddCustomFunction with an instance of the custom function class.


    The spreadsheet pretty much follows Excel's rules for when a string values can be passed to operators or functions expecting number values.  For example, Excel's + operator converts string arguments to number values but SUM function ignores string arguments passed by reference.  Spread does the same...


                DefaultSheetDataModel model = (DefaultSheetDataModel)fpSpread1.Sheets[0].Models.Data;
                model.SetValue(0, 0, "1");
                model.SetValue(1, 0, "2");
                model.SetFormula(2, 0, "A1+A2"); // evaluates to 3
                model.SetFormula(3, 0, "SUM(A1,A2)"); // evaluates to 0

  • Replied 8 September 2017, 2:06 pm EST

    Thanks, bobbyo.

    Only one comment: I have checked "A1+A2" and "SUM(A1,A2)" functions into Excel for A1="1" and A2="2" => SUM(A1,A2) = 0, BUT A1+A2=#VALUE!

     

  • Replied 8 September 2017, 2:06 pm EST

    Anna,


    I checked both Excel 2000 and Excel 2007 (the two versions of Excel that I have on my computer).  In both versions, if A1 = "1" and A2 = "2" then SUM(A1,A2) = 0 and A1+A2 = 3.  I am not sure what you and I are doing differently in Excel to get different results for A1+A2.


    Note that Excel uses your computer's regional settings for converting strings to numbers.  If A1 = "1.0" and A2 = "2.0" then A1+A2 = 3 with "English (United States)" regional setting which uses period "." for decimal symbol but A1+A2 = #VALUE! with "German (Germany)" regional setting which uses comma "," for decimal symbol.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels