Export (0) Print
Expand All
This topic has not yet been rated - 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.

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

Namespace:  System.CodeDom
Assembly:  System (in System.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
public class CodeIterationStatement : CodeStatement

The CodeIterationStatement type exposes the following members.

  NameDescription
Public methodCodeIterationStatement()Initializes a new instance of the CodeIterationStatement class.
Public methodCodeIterationStatement(CodeStatement, CodeExpression, CodeStatement, CodeStatement[])Initializes a new instance of the CodeIterationStatement class using the specified parameters.
Top
  NameDescription
Public propertyEndDirectivesGets a CodeDirectiveCollection object that contains end directives. (Inherited from CodeStatement.)
Public propertyIncrementStatementGets or sets the statement that is called after each loop cycle.
Public propertyInitStatementGets or sets the loop initialization statement.
Public propertyLinePragmaGets or sets the line on which the code statement occurs. (Inherited from CodeStatement.)
Public propertyStartDirectivesGets a CodeDirectiveCollection object that contains start directives. (Inherited from CodeStatement.)
Public propertyStatementsGets the collection of statements to be executed within the loop.
Public propertyTestExpressionGets or sets the expression to test as the condition that continues the loop.
Public propertyUserDataGets the user-definable data for the current object. (Inherited from CodeObject.)
Top
  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.