public void ThenTheBalanceInMyAccountIs(int finalBalance) { CalculatorClass Cal = new CalculatorClass(AccountBalance, WithDrawAmount); FinalBalance = Cal.Calculate(); Assert.AreEqual(FinalBalance, finalBalance); }
public override void OnClick() { if (Yutai.ArcGIS.Common.Editor.Editor.CheckIsEdit(_menuStrip.TableView.FeatureLayer)) { ITable pTable = _menuStrip.TableView.VirtualGridView.FeatureLayer.FeatureClass as ITable; IField pField = pTable.Fields.Field[_menuStrip.ColumnIndex]; FieldCalculator frm = new FieldCalculator(_menuStrip.TableView.FeatureLayer.FeatureClass.Fields, pField); if (frm.ShowDialog() == DialogResult.OK) { try { ICalculator pCalculator = new CalculatorClass(); ICursor pCursor = pTable.Search(null, false); pCalculator.Cursor = pCursor; pCalculator.Field = pField.Name; pCalculator.Expression = frm.Expression; pCalculator.Calculate(); Marshal.ReleaseComObject(pCursor); } catch (Exception exception) { MessageBox.Show(exception.Message); } _menuStrip.TableView.VirtualGridView.ShowTable(null); } } else { MessageBox.Show(@"未启动编辑", Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
/// <summary> /// Performs the calculation by executing the pre-expression and expression. /// </summary> /// <param name="source">The source.</param> /// <param name="filter">The filter used to access the rows on which the calculation will be performed.</param> /// <param name="fieldName">Field to perform the calculation on.</param> /// <param name="expression">Expression or value applied to a field in each row of the cursor.</param> /// <param name="preExpression"> /// A pre-calculation determination of a value or variable that may be used as the expression /// (or value) of the calculation. /// </param> /// <param name="showErrorPrompt">if set to <c>true</c> show a message prompt when an error occurs during calculation.</param> /// <param name="callback">The call back routine.</param> /// <returns> /// Returns a <see cref="IEnvelope" /> representing the features that have been modified by the calculation. This /// envelope can be used to refresh the display for the calculated area only. If the table used in the calculate is /// non-spatial, a null is returned. /// </returns> public static IEnvelope Calculate(this ITable source, IQueryFilter filter, string fieldName, string expression, string preExpression, bool showErrorPrompt, ICalculatorCallback callback) { ICalculator calculator = new CalculatorClass(); calculator.Callback = callback; calculator.Cursor = source.Search(filter, false); calculator.Field = fieldName; calculator.Expression = expression; calculator.PreExpression = preExpression; calculator.ShowErrorPrompt = showErrorPrompt; return(calculator.Calculate()); }
private void setDynamicDefaults(IObject inObject, string mode) { try { //Convert row to feature (test for feature is null before using - this could be a table update) IFeature inFeature = inObject as IFeature; // Skip Orphan Junctions (saves time) if (inFeature != null) { INetworkFeature inNetFeat = inObject as INetworkFeature; if ((inNetFeat != null) && (inFeature.Class.ObjectClassID == inNetFeat.GeometricNetwork.OrphanJunctionFeatureClass.ObjectClassID)) return; } //Get cursor to dynamic values table retriving only ICursor tabCursor; getDefaultRows(inObject, out tabCursor); IRow row = null; if (tabCursor != null) row = tabCursor.NextRow(); //for each row in the matching rows (matched by table name or wildcard) returned from the config table while (row != null) { //get fieldname string fieldName = row.get_Value(dynTargetField).ToString(); //if this field is found in the feature/object being added or modified... int fieldNum = inObject.Fields.FindField(fieldName); if (fieldNum > -1) { // get requested method and any data parameters string valMethod = row.get_Value(dynMethodField).ToString(); string valData = row.get_Value(dynDataField).ToString(); switch (mode) { case "OnCreate": //Continue to next field in config table if create events were not requested if (row.get_Value(dynCreateField).ToString() == "0") { row = tabCursor.NextRow(); continue; } break; case "OnChange": // Collect value for changed feature (stored for LAST VALUE method) IRowChanges inChanges = inObject as IRowChanges; bool changed = inChanges.get_ValueChanged(fieldNum); if (changed) lastValueProperties.SetProperty(fieldName, inObject.get_Value(fieldNum)); //Continue to next field in config table if change events were not requested if (row.get_Value(dynChangeField).ToString() == "0") { row = tabCursor.NextRow(); continue; } break; } // set values as specified switch (valMethod) { case "TIMESTAMP": inObject.set_Value(fieldNum, DateTime.Now); break; case "LAST_VALUE": if (mode == "OnCreate") { //if (inObject.get_Value(fieldNum) == null) //{ object lastValue = lastValueProperties.GetProperty(fieldName); if (lastValue != null) inObject.set_Value(fieldNum, lastValue); //} } break; case "FIELD": // verify that field to copy exists int fieldCopy = inObject.Fields.FindField(valData as string); if (fieldCopy > -1) { //copy value only if current field is empty string currentValue = inObject.get_Value(fieldNum).ToString(); if (currentValue == "") inObject.set_Value(fieldNum, inObject.get_Value(fieldCopy)); } break; case "CURRENT_USER": if (lastEditorName == null) lastEditorName = getCurrentUser(); inObject.set_Value(fieldNum, lastEditorName); break; case "GUID": if (mode == "OnCreate") // SHould only set this once on create to give the object a unique value { object currentValue = inObject.get_Value(fieldNum); if (DBNull.Value == currentValue) // Do not overwrite if someone else has already generated { Guid g = Guid.NewGuid(); inObject.set_Value(fieldNum, g.ToString("B").ToUpper()); } } break; case "EXPRESSION": if (mode == "OnCreate") { if (inFeature != null & valData != null) { try { int calcField = inFeature.Fields.FindField(fieldName); //if (inFeature.get_Value(calcField) == null) //{ int[] fids = { inFeature.OID }; IGeoDatabaseBridge gdbBridge = new GeoDatabaseHelperClass(); IFeatureCursor fCursor = (IFeatureCursor)gdbBridge.GetFeatures((IFeatureClass)inFeature.Class, ref fids, false); ICalculator calc = new CalculatorClass(); calc.Expression = valData; calc.Field = fieldName; calc.Cursor = (ICursor)fCursor; calc.ShowErrorPrompt = false; ICalculatorCallback calculatorCallback = new CalculatorCallback(); calc.Callback = calculatorCallback; calc.Calculate(); calculatorCallback = null; //} } catch { } } } break; default: break; } } row = tabCursor.NextRow(); } if (null != tabCursor) { System.Runtime.InteropServices.Marshal.ReleaseComObject(tabCursor); } } catch (Exception ex) { _logHelper.addLogEntry(DateTime.Now.ToString(), "ERROR", "Error applying dynamic defaults.", ex.ToString()); MessageBox.Show("Error: \n" + ex.ToString()); } }
public void calculate_calculationtype_Invalid() { Assert.ThrowsException <ArgumentException>(() => { CalculatorClass.Calculate(1, 1, null); }); }
public void calculate_Multiply_Valid() { Assert.IsTrue(CalculatorClass.Calculate(2, 6, CalculatorClass.CalculationType.Multiply) == 12); }
public void calculate_Divide_Valid() { Assert.IsTrue(CalculatorClass.Calculate(2, 2, CalculatorClass.CalculationType.Divide) == 1); }
public void calculate_Add_Valid() { Assert.IsTrue(CalculatorClass.Calculate(1, -1, CalculatorClass.CalculationType.Add) == 0); }
private void setDynamicDefaults(IObject inObject, string mode) { try { //Convert row to feature (test for feature is null before using - this could be a table update) IFeature inFeature = inObject as IFeature; // Skip Orphan Junctions (saves time) if (inFeature != null) { INetworkFeature inNetFeat = inObject as INetworkFeature; if ((inNetFeat != null) && (inFeature.Class.ObjectClassID == inNetFeat.GeometricNetwork.OrphanJunctionFeatureClass.ObjectClassID)) { return; } } //Get cursor to dynamic values table retriving only ICursor tabCursor; getDefaultRows(inObject, out tabCursor); IRow row = null; if (tabCursor != null) { row = tabCursor.NextRow(); } //for each row in the matching rows (matched by table name or wildcard) returned from the config table while (row != null) { //get fieldname string fieldName = row.get_Value(dynTargetField).ToString(); //if this field is found in the feature/object being added or modified... int fieldNum = inObject.Fields.FindField(fieldName); if (fieldNum > -1) { // get requested method and any data parameters string valMethod = row.get_Value(dynMethodField).ToString(); string valData = row.get_Value(dynDataField).ToString(); switch (mode) { case "OnCreate": //Continue to next field in config table if create events were not requested if (row.get_Value(dynCreateField).ToString() == "0") { row = tabCursor.NextRow(); continue; } break; case "OnChange": // Collect value for changed feature (stored for LAST VALUE method) IRowChanges inChanges = inObject as IRowChanges; bool changed = inChanges.get_ValueChanged(fieldNum); if (changed) { lastValueProperties.SetProperty(fieldName, inObject.get_Value(fieldNum)); } //Continue to next field in config table if change events were not requested if (row.get_Value(dynChangeField).ToString() == "0") { row = tabCursor.NextRow(); continue; } break; } // set values as specified switch (valMethod) { case "TIMESTAMP": inObject.set_Value(fieldNum, DateTime.Now); break; case "LAST_VALUE": if (mode == "OnCreate") { //if (inObject.get_Value(fieldNum) == null) //{ object lastValue = lastValueProperties.GetProperty(fieldName); if (lastValue != null) { inObject.set_Value(fieldNum, lastValue); } //} } break; case "FIELD": // verify that field to copy exists int fieldCopy = inObject.Fields.FindField(valData as string); if (fieldCopy > -1) { //copy value only if current field is empty string currentValue = inObject.get_Value(fieldNum).ToString(); if (currentValue == "") { inObject.set_Value(fieldNum, inObject.get_Value(fieldCopy)); } } break; case "CURRENT_USER": if (lastEditorName == null) { lastEditorName = getCurrentUser(); } inObject.set_Value(fieldNum, lastEditorName); break; case "GUID": if (mode == "OnCreate") // SHould only set this once on create to give the object a unique value { object currentValue = inObject.get_Value(fieldNum); if (DBNull.Value == currentValue) // Do not overwrite if someone else has already generated { Guid g = Guid.NewGuid(); inObject.set_Value(fieldNum, g.ToString("B").ToUpper()); } } break; case "EXPRESSION": if (mode == "OnCreate") { if (inFeature != null & valData != null) { try { int calcField = inFeature.Fields.FindField(fieldName); //if (inFeature.get_Value(calcField) == null) //{ int[] fids = { inFeature.OID }; IGeoDatabaseBridge gdbBridge = new GeoDatabaseHelperClass(); IFeatureCursor fCursor = (IFeatureCursor)gdbBridge.GetFeatures((IFeatureClass)inFeature.Class, ref fids, false); ICalculator calc = new CalculatorClass(); calc.Expression = valData; calc.Field = fieldName; calc.Cursor = (ICursor)fCursor; calc.ShowErrorPrompt = false; ICalculatorCallback calculatorCallback = new CalculatorCallback(); calc.Callback = calculatorCallback; calc.Calculate(); calculatorCallback = null; //} } catch { } } } break; default: break; } } row = tabCursor.NextRow(); } if (null != tabCursor) { System.Runtime.InteropServices.Marshal.ReleaseComObject(tabCursor); } } catch (Exception ex) { _logHelper.addLogEntry(DateTime.Now.ToString(), "ERROR", "Error applying dynamic defaults.", ex.ToString()); MessageBox.Show("Error: \n" + ex.ToString()); } }