This documentation is archived and is not being maintained.

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.

For a list of all members of this type, see CodeIterationStatement Members.

System.Object
   System.CodeDom.CodeObject
      System.CodeDom.CodeStatement
         System.CodeDom.CodeIterationStatement

[Visual Basic]
<Serializable>
<ClassInterface(ClassInterfaceType.AutoDispatch)>
<ComVisible(True)>
Public Class CodeIterationStatement
   Inherits CodeStatement
[C#]
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ComVisible(true)]
public class CodeIterationStatement : CodeStatement
[C++]
[Serializable]
[ClassInterface(ClassInterfaceType::AutoDispatch)]
[ComVisible(true)]
public __gc class CodeIterationStatement : public CodeStatement
[JScript]
public
   Serializable
 ClassInterface(ClassInterfaceType.AutoDispatch)
 ComVisible(true)
class CodeIterationStatement extends CodeStatement

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

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.

Example

[Visual Basic, C#, C++] This example demonstrates using a CodeIterationStatement to represent a for loop.

[Visual Basic] 
' Declares and initializes an integer variable named testInt.
Dim testInt As New CodeVariableDeclarationStatement(GetType(Integer), "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.
   ' initStatement parameter for pre-loop initialization.
   ' testExpression parameter indicates the epxression to test for continuation condition.
   ' incrementStatement parameter indicates statement to execute after each iteration.
   ' 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.

Dim forLoop As New CodeIterationStatement( _
   New CodeAssignStatement(New CodeVariableReferenceExpression("testInt"), New CodePrimitiveExpression(1)), _
   New CodeBinaryOperatorExpression(New CodeVariableReferenceExpression("testInt"), _ 
       CodeBinaryOperatorType.LessThan, New CodePrimitiveExpression(10)), _
   New CodeAssignStatement(New CodeVariableReferenceExpression("testInt"), _
   New CodeBinaryOperatorExpression(New CodeVariableReferenceExpression("testInt"), _
       CodeBinaryOperatorType.Add, New CodePrimitiveExpression(1))), _
   New CodeStatement() {New CodeExpressionStatement( _
       New CodeMethodInvokeExpression(New CodeMethodReferenceExpression(New CodeTypeReferenceExpression("Console"), "WriteLine"), _ 
               New CodeMethodInvokeExpression(New CodeVariableReferenceExpression("testInt"), "ToString")))})


        ' A Visual Basic code generator produces the following source code for the preceeding example code:

        '     Dim testInt As Integer = 0
        '     testInt = 1
        '     Do While (testInt < 10)
        '         Console.WriteLine(testInt.ToString)
        '         testInt = (testInt + 1)

[C#] 
// 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());

[C++] 
// Declares and initializes an integer variable named testInt.
CodeVariableDeclarationStatement* testInt = new CodeVariableDeclarationStatement(__typeof(int), S"testInt", new CodePrimitiveExpression(0) );

CodeMethodInvokeExpression* writelineparams[] = {
    new CodeMethodInvokeExpression(
        new CodeVariableReferenceExpression(S"testInt"),
        S"ToString",
        0)
};

CodeStatement* codestatements[] = {
    new CodeExpressionStatement(
        new CodeMethodInvokeExpression(
        new CodeMethodReferenceExpression( 
        new CodeTypeReferenceExpression(S"Console"),
        S"WriteLine"),
        writelineparams)) 
};

// 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(S"testInt"), new CodePrimitiveExpression(__box(1)) ),
    // testExpression parameter to test for continuation condition.
    new CodeBinaryOperatorExpression( new CodeVariableReferenceExpression(S"testInt"), 
    CodeBinaryOperatorType::LessThan, new CodePrimitiveExpression(__box(10)) ),
    // incrementStatement parameter indicates statement to execute after each iteration.
    new CodeAssignStatement( new CodeVariableReferenceExpression(S"testInt"), new CodeBinaryOperatorExpression( 
    new CodeVariableReferenceExpression(S"testInt"), CodeBinaryOperatorType::Add, new CodePrimitiveExpression(__box(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.
    codestatements );

// 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());

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.CodeDom

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

CodeIterationStatement Members | System.CodeDom Namespace

Show: