/// <summary> /// This is a helper method that looks up the value of a given cell /// its main use is when cells are created. Here when in the creation of a cell /// we want to immidiately use lookup as the delegete for the evaluator method in formula /// </summary> /// <returns></returns> private double lookup(string nameOfCell) { //name is converted to upper nameOfCell = nameOfCell.ToUpper(); if (!cellDictionary.ContainsKey(nameOfCell)) { throw new FormulaEvaluationException("The variable could not be found in the spreadsheet"); } Cell tempCell = cellDictionary[nameOfCell]; if (tempCell.GetValue() is double) { return((double)tempCell.GetValue()); } else { throw new FormulaEvaluationException("Lookup could not be properly performed."); } }
public override object GetCellValue(string name) { // Normalize our cell name string cellName = Normalize(name); // If name is null or invalid. CheckCellName(cellName); if (Cells.ContainsKey(cellName)) { Cell temp = Cells[cellName]; return(temp.GetValue()); } return(""); }
// Private helper methods for Spreadsheet /// <summary> /// Private helper method that uses our dictionary "Cells" in order to /// lookup a cell's value for evaluation. /// This method will throw an arguement exception if: /// - A Cell is empty (value is empty string) /// - A cell's value is a string / formula error /// This in turn will cause an evaluation in the Formula Class of this cell /// to result in a return of a FormulaError. /// </summary> /// <param name="name">Cell Name</param> /// <returns>Cell's double value.</returns> private double lookup(string name) { // Looking up a cell that hasnt been added yet, add an empty one. if (!Cells.ContainsKey(name)) { Cells.Add(name, new Cell(name, "")); } Cell cell = Cells[name]; object value = cell.GetValue(); // We have a string or FormulaError as the value of our cell. if (value is string || value is FormulaError) { throw new ArgumentException(); } // Else we know our cell value is a double and we can return it as such. return((double)value); }