private void UpdateCellValue(ref SpreadsheetCell cell) { var mainTree = new ExpTree(); // Initializes a new expression tree to build the cell's expression ErrType Error = ErrType.None; if (cell.Text != "" && cell.Text[0] != '=') // not an expression, simply a text value { cellArr[cell.RowIndex, cell.ColumnIndex] = new Cell(cell, cell.Text); add_remove(cell, mainTree, true); } else { if (cell.Text != "") mainTree.Expression = cell.Text.Substring(1).Replace(" ", ""); // Build the expression tree with the cell's text (minus the '=') :: Also ignores whitespaces else mainTree.Expression = cell.Text; add_remove(cell, mainTree, true); // Removes all variables cooresponding to the old tree cell.VarList = GetVarNames(cell.Text); // Will Return all found variables in the new expression Error = add_remove(cell, mainTree, false); if (Error != ErrType.None) // Notifies the UI that there is an error in one of the cells that the expression references { return; // Exits the function before executing anything else, error display has already been taken care of at this point } try { cellArr[cell.RowIndex, cell.ColumnIndex] = new Cell(cell, mainTree.Eval().ToString()); // Attempts to evaluate the expression, placing it into a new cell } catch (DivideByZeroException) // User tried to divide by zero { CheckErr(cell, ErrType.DivZero, CellToString(cell)); UpdateErrorReferecedBy(cell, ErrType.DivZero, CellToString(cell)); return; } catch (NullReferenceException) // Input not regonized / invalid expression { if (cell.Text == "") { cellArr[cell.RowIndex, cell.ColumnIndex] = new Cell(cell, ""); // if the cell was deleted or reset, this will set the cell to an empty value (caught by expression tree as null) } else { CheckErr(cell, ErrType.InvExp, CellToString(cell)); UpdateErrorReferecedBy(cell, ErrType.InvExp, CellToString(cell)); // Notifies UI that an invalid expression has been entered return; } } } cellArr[cell.RowIndex, cell.ColumnIndex].PropertyChanged += detect_PropertyChanged; // Reassigns the the detect_property function to the cell's delegate CellPropertyChanged(cellArr[cell.RowIndex, cell.ColumnIndex], new PropertyChangedEventArgs("Value")); // fires the event that notifies the GUI of a change UpdateReferencedBy(cell); // Updates all cells that reference this cell }