public static void delegatesMain() { //delegate object must be instantiated with new keyword and a particular method NumberOperator n1 = new NumberOperator(addNum); NumberOperator n2 = new NumberOperator(mulNum); Console.WriteLine("n1(25) = " + n1(25)); Console.WriteLine("n2(20) = " + n2(20)); num = 10; //delegate objects allows its component composed using '+' and removed using '-' //creation of list of invocation of method through multicasting NumberOperator multicast; multicast = n1; multicast += n2; multicast -= n1; Console.WriteLine("n1+n2-n1(20) = " + multicast(20)); //pass delegate object into a function which invokes the relevant function printString ps1 = new printString(writeToScreen); printString ps2 = new printString(writeToFile); sendString(ps1); sendString(ps2); }
private static IOperand CreateOperend(XElement operandElement) { XAttribute type = operandElement.Attribute("Type"); IOperand operand = null; switch (type.Value) { case "NumberOperator": operand = new NumberOperator(int.Parse(operandElement.Attribute("Value").Value)); break; default: string qualifyingName = "CalculatorTests." + type.Value; operand = Activator.CreateInstance(Type.GetType(qualifyingName)) as IOperand; List <XNode> childNodes = new List <XNode>(operandElement.Nodes()); if (operand is BinaryOperator) { BinaryOperator binaryOperator = operand as BinaryOperator; binaryOperator.Left = CreateOperend(childNodes[0] as XElement); binaryOperator.Right = CreateOperend(childNodes[1] as XElement); } else if (operand is UnaryOperator) { UnaryOperator unaryOperator = operand as UnaryOperator; unaryOperator.Operand = CreateOperend(childNodes[0] as XElement); } break; } return(operand); }
public Argument Evaluate() { if (Arguments[1].CanEvaluate) { INumericalTypeOperator numericalOperator = new NumberOperator(); NumericalOperator operation = (NumericalOperator)Arguments[1].Evaluateble.Evaluate().Value; // Numbers default to 0, if an argument is not given object l = Arguments[0].Evaluateble?.Evaluate().Value ?? 0; object r = Arguments[2].Evaluateble?.Evaluate().Value ?? 0; // If the operation is a division and the divisor is 0, throw a DivideByZeroException if (operation == NumericalOperator.Division && (float)r == 0) { throw new System.DivideByZeroException(); } object value = EvaluateOperation(l, r, numericalOperator, operation); return(new Argument(value, ResultingType)); } else { throw new CodeblockOperatorException("The codeblock arguments do not contain an operation."); } }
static void Main(string[] args) { NumberOperator numberOperator = Math.Add; // Delegates are containers for functions System.Console.WriteLine("Hello World!"); var vehicles = new List <Vehicle>(); var myVehicles = new MyList <Vehicle>(); }
internal void Start() { StringConcatinator stringConcatinator = StringConcatinatorMethod; NumberOperator numberOperator = NumberOperatorMethod; Console.WriteLine(stringConcatinator(stringCollection)); bool selector = true; Console.WriteLine(numberOperator(floatCollection, selector)); }
public NumberParam(T value, NumberOperator numberOperator) { if (typeof(T).IsClass && value == null) { throw new NotNullableException("NumberParam cannot be null"); } FOperator = numberOperator; FValue = value; if (numberOperator == NumberOperator.Increment || numberOperator == NumberOperator.Decrement || numberOperator == NumberOperator.Add || numberOperator == NumberOperator.Subtract) { IsNull = false; } }
public NullableNumberParam(T value, NumberOperator numberOperator) { if (typeof(T).IsClass && value == null) { FOperator = NumberOperator.EqualsNull; IsNull = false; return; } FOperator = numberOperator; FValue = value; if (numberOperator == NumberOperator.EqualsNull || numberOperator == NumberOperator.Increment || numberOperator == NumberOperator.Decrement || numberOperator == NumberOperator.Add || numberOperator == NumberOperator.Subtract) { IsNull = false; } }
public Argument Evaluate() { if (!Arguments[0].CanEvaluate || !Arguments[2].CanEvaluate) { throw new CodeblockOperatorException("The codeblock arguments can not be empty."); } object l = Arguments[0].Evaluateble.Evaluate().Value; object r = Arguments[2].Evaluateble.Evaluate().Value; // Get the types of the arguments ArgumentType lType = ArgumentTypeHelper.FromObject(l); ArgumentType rType = ArgumentTypeHelper.FromObject(r); // Ensure the arguments don't have different types if (lType != rType) { throw new CodeblockOperatorException("The codeblock arguments must have the same type."); } // Use the left argument to see which type the comparison should take IComparisonTypeOperator comparisonOperator; ComparisonOperator operation = (ComparisonOperator)Arguments[1].Evaluateble.Evaluate().Value; switch (lType) { case ArgumentType.Number: comparisonOperator = new NumberOperator(); break; case ArgumentType.String: comparisonOperator = new StringOperator(); break; case ArgumentType.Boolean: comparisonOperator = new BooleanOperator(); break; default: throw new CodeblockOperatorException($"No valid operator found for argument type {lType}."); } object result = EvaluateOperation(l, r, comparisonOperator, operation); return(new Argument(result, ResultingType)); }
public void PriceCalculation(NumberOperator inputFromMainMenuChoice) { Console.WriteLine("${0}", inputFromMainMenuChoice(products)); }
/// <summary> /// Get the next movie number /// </summary> /// <param name="op">The op.</param> /// <returns></returns> public int GetMovieNumber(NumberOperator op) { int numCount = 0; //int numMax = 0; SqlDataReader reader; StringBuilder str = new StringBuilder(); // get count try { if(op == NumberOperator.Min) { str.Append("SELECT MIN(number) FROM tbl_movies"); } else if(op == NumberOperator.Max) { str.Append("SELECT MAX(number) FROM tbl_movies"); } this._db.OpenConnection(); reader = (SqlDataReader)this._db.ExecuteQueryForDataReader( str.ToString(), CommandType.Text ); if(reader != null && reader.HasRows) { reader.Read(); numCount = reader.GetInt32(0); } } catch(Exception) { numCount = 0; } finally { this._db.CloseConnection(); } return numCount;//+1; }