This project has moved and is read-only. For the latest updates, please go here.

foreach needs { and } around in EE V2.0.1.0

May 2, 2014 at 9:54 PM
I am not sure if this is an issue or by design. When using a 'foreach' loop if the embedded statement(s) are not surrounded by { and } the statement(s) are not executed.

This does not happen when using a 'for' loop.


This is running against EE 2.0.1.0 that was uploaded on April 30
using System;
using ExpressionEvaluator;

namespace Tests3
{
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();

            // Do it the regular way
            double sum = 0.0; foreach (double x in a.Doubles) sum = sum + x; Console.WriteLine("sum (C#): " + sum);

            // Now in EE
            var registry = new TypeRegistry();
            registry.RegisterSymbol("a", a);

            // for loop - works
            //string script = "double sum = 0.0; for (int i = 0; i < a.Doubles.Length; i++) sum = sum + a.Doubles[i]; sum;";

            // foreach loop with { and } - works
            //string script = "double sum = 0.0; foreach (double x in a.Doubles) { sum = sum + x; } sum;";

            // foreach loop without { and } - does not work, returns 0.0
            string script = "double sum = 0.0; foreach (double x in a.Doubles) sum = sum + x; sum;";

            var cc = new CompiledExpression() { StringToParse = script, TypeRegistry = registry };
            cc.ExpressionType = CompiledExpressionType.StatementList;
            Console.WriteLine("sum (EE): " + cc.Eval());

            Console.ReadLine();
        }
    }

    public class A
    {
        public double[] Doubles { get; set; }

        public A()
        {
            Doubles = new double[2] {1.11, 2.22};
        }
    }
}
May 3, 2014 at 12:56 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
May 3, 2014 at 1:07 AM
Edited May 3, 2014 at 1:50 AM
Thanks for catching this.

I've found the problem and fixed it. The fix has been included in the latest commit.