Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

CodeIterationStatement Class

Represents a for statement, or a loop through a block of statements, using a test expression as a condition for continuing to loop.

Namespace: System.CodeDom
Assembly: System (in system.dll)

[SerializableAttribute] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public class CodeIterationStatement : CodeStatement
/** @attribute SerializableAttribute() */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
/** @attribute ComVisibleAttribute(true) */ 
public class CodeIterationStatement extends CodeStatement
SerializableAttribute 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
ComVisibleAttribute(true) 
public class CodeIterationStatement extends CodeStatement
Not applicable.

A CodeIterationStatement can represent a for loop or while loop.

The InitStatement property specifies the statement to execute before the first loop iteration. The TestExpression property specifies the loop continuation expression, which must evaluate to true at the end of each loop iteration for another iteration to begin. The IncrementStatement property specifies the statement to execute at the end of each loop iteration. The Statements property specifies the collection of statements to execute within the loop.

This example demonstrates using a CodeIterationStatement to represent a for loop.

// Declares and initializes an integer variable named testInt.
CodeVariableDeclarationStatement testInt = new CodeVariableDeclarationStatement(typeof(int), "testInt", new CodePrimitiveExpression(0) );

// Creates a for loop that sets testInt to 0 and continues incrementing testInt by 1 each loop until testInt is not less than 10.
CodeIterationStatement forLoop = new CodeIterationStatement(
    // initStatement parameter for pre-loop initialization.
    new CodeAssignStatement( new CodeVariableReferenceExpression("testInt"), new CodePrimitiveExpression(1) ),
    // testExpression parameter to test for continuation condition.
    new CodeBinaryOperatorExpression( new CodeVariableReferenceExpression("testInt"), 
        CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression(10) ),
    // incrementStatement parameter indicates statement to execute after each iteration.
    new CodeAssignStatement( new CodeVariableReferenceExpression("testInt"), new CodeBinaryOperatorExpression( 
        new CodeVariableReferenceExpression("testInt"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1) )),
    // statements parameter contains the statements to execute during each interation of the loop.
    // Each loop iteration the value of the integer is output using the Console.WriteLine method.
    new CodeStatement[] { new CodeExpressionStatement( new CodeMethodInvokeExpression( new CodeMethodReferenceExpression( 
        new CodeTypeReferenceExpression("Console"), "WriteLine" ), new CodeMethodInvokeExpression( 
        new CodeVariableReferenceExpression("testInt"), "ToString" ) ) ) } );

// A C# code generator produces the following source code for the preceeding example code:

//     int testInt = 0;
//     for (testInt = 1; (testInt < 10); testInt = (testInt + 1)) {
//        Console.WriteLine(testInt.ToString());

// Declares and initializes an integer variable named testInt.
CodeVariableDeclarationStatement testInt = new 
    CodeVariableDeclarationStatement(int.class.ToType(), 
    "testInt", new CodePrimitiveExpression((Int32)0));
// Creates a for loop that sets testInt to 0 and continues 
// incrementing testInt by 1 each loop until testInt is not less than 10.
CodeIterationStatement forLoop = new CodeIterationStatement(
    // initStatement parameter for pre-loop initialization.
    new CodeAssignStatement(new CodeVariableReferenceExpression(
    "testInt"),new CodePrimitiveExpression((Int32)1)),
    // testExpression parameter to test for continuation condition.
    new CodeBinaryOperatorExpression(new 
    CodeVariableReferenceExpression("testInt"), 
    CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression(
    (Int32)10)),
    // incrementStatement parameter indicates statement to execute after
    // each iteration.
    new CodeAssignStatement(new 
    CodeVariableReferenceExpression("testInt"), new 
    CodeBinaryOperatorExpression(new 
    CodeVariableReferenceExpression("testInt"), 
    CodeBinaryOperatorType.Add, new CodePrimitiveExpression((Int32)1))),
    // statements parameter contains the statements to execute during each
    // interation of the loop.
    // Each loop iteration the value of the integer is output using the
    // Console.WriteLine method.
    new CodeStatement[] { new CodeExpressionStatement(new 
    CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new
    CodeTypeReferenceExpression("Console"), "WriteLine"), 
    new CodeExpression [] { new CodeMethodInvokeExpression(new 
    CodeVariableReferenceExpression("testInt"), "ToString", 
    new CodeExpression[] {}) })) });
    // A VJ# code generator produces the following source code for 
    // the preceeding example code:
    //     int testInt = 0;
    //     for (testInt = 1; testInt < 10; testInt = testInt + 1) {
    //        Console.WriteLine(System.Convert.ToString(testInt));

System.Object
   System.CodeDom.CodeObject
     System.CodeDom.CodeStatement
      System.CodeDom.CodeIterationStatement
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.