AjScript.Commands.ForCommand.Execute C# (CSharp) Метод

Execute() публичный Метод

public Execute ( IContext context ) : void
context IContext
Результат void
        public void Execute(IContext context)
        {
            if (this.initialCommand != null)
                this.initialCommand.Execute(context);

            while (this.condition == null || Predicates.IsTrue(this.condition.Evaluate(context)))
            {
                if (this.body != null)
                    this.body.Execute(context);
                if (this.endCommand != null)
                    this.endCommand.Execute(context);
            }
        }

Usage Example

Пример #1
0
        public void ExecuteForCommand()
        {
            ICommand setX = new SetVariableCommand("x", new ConstantExpression(0));
            ICommand setY = new SetVariableCommand("y", new ConstantExpression(0));
            List<ICommand> commands = new List<ICommand>();
            commands.Add(setX);
            commands.Add(setY);
            ICommand initialCommand = new CompositeCommand(commands);

            IExpression condition = new CompareExpression(ComparisonOperator.Less, new VariableExpression("x"), new ConstantExpression(6));

            IExpression addXtoY = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("y"), new VariableExpression("x"));
            ICommand addToY = new SetVariableCommand("y", addXtoY);

            ICommand endCommand = new SetVariableCommand("x", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("x"), new ConstantExpression(1)));

            ForCommand forcmd = new ForCommand(initialCommand, condition, endCommand, addToY);

            Context context = new Context();

            context.SetValue("y", null);

            forcmd.Execute(context);

            Assert.AreEqual(15, context.GetValue("y"));
        }