[stringtemplate-interest] Problem with IEnumerator collections (c# 2.3b7 )

Kenny Cockburn kenny.cockburn at ntlworld.com
Fri Jun 2 09:21:55 PDT 2006


I create a simple class that only implements the IEnumerator interface ;

I have a template that test if the collection has rows then prints the 
collections values

In the case where we have an array everything works as expected  in the 
case of the IEnumerator the rest() method is not called in the $if$ test 
so the list starts on the second element also after the list has been 
consumed the Rest() method is not called so subsequent  use of the list 
results in an empty list

see the code that follows



---------
        class myIEnum : IEnumerator
        {
            int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
            IEnumerator itor;
            public myIEnum()
            {
                itor = myIntArray.GetEnumerator();                   
            }
            #region IEnumerator Members       
            public object Current
            {
                get
                {
                    return itor.Current;
                }
            }
            public bool MoveNext()
            {
                return itor.MoveNext();           
            }
            public void Reset()
            {
                itor.Reset();
            }
            #endregion
        }

        static void Main()
        {
  
            string simpleIfTest = @"
group TestIEnum ;

Action(Arg) ::=<<
$if( Arg )$
   $Arg:{X=$it$ # }$
$endif$
----                                                    /
Without If Test
$Arg:{X=$it$ # }$
----
 >>
";
            StringTemplateGroup stg = new StringTemplateGroup(new 
StringReader(simpleIfTest), typeof(DefaultTemplateLexer));
            StringTemplate sta3 = stg.GetInstanceOf("Action");
            int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
           
            System.Console.WriteLine("Run Test with simple Array");
            sta3.SetAttribute("Arg", myIntArray );
            System.Console.WriteLine(sta3.ToString());

            System.Console.WriteLine("Run Test with type only supporting 
IEnumerator");        
            myIEnum Inum = new myIEnum();
            sta3 = stg.GetInstanceOf("Action");
            sta3.SetAttribute("Arg", Inum);
            System.Console.WriteLine(sta3.ToString());

            // Re-create object so no side effect from previous calls
            Inum = new myIEnum();
            System.Console.WriteLine("Prove IEnumerator Class");
            IEnumerator itor = (IEnumerator)Inum;
            while ( itor.MoveNext() )
            {
                System.Console.WriteLine("Num = " + (int)itor.Current );
            }
            itor.Reset();

            System.Console.WriteLine("Run Loop for second time Prove 
IEnumerator Class");
            while (itor.MoveNext())
            {
                System.Console.WriteLine("Num = " + (int)itor.Current);
            }
            itor.Reset();
    }





More information about the stringtemplate-interest mailing list