Freigeben über


CodeIterationStatement-Klasse

Stellt eine for-Anweisung oder eine Schleife durch einen Anweisungsblock dar, wobei ein Testausdruck als Bedingung für die Fortsetzung der Schleife verwendet wird.

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

Syntax

'Declaration
<SerializableAttribute> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
<ComVisibleAttribute(True)> _
Public Class CodeIterationStatement
    Inherits CodeStatement
'Usage
Dim instance As CodeIterationStatement
[SerializableAttribute] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public class CodeIterationStatement : CodeStatement
[SerializableAttribute] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
[ComVisibleAttribute(true)] 
public ref class CodeIterationStatement : public 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

Hinweise

Ein CodeIterationStatement kann eine for-Schleife oder eine while-Schleife darstellen.

Die InitStatement-Eigenschaft gibt die vor der ersten Schleifeniteration auszuführende Anweisung an. Die TestExpression-Eigenschaft gibt den Ausdruck für die Fortsetzung der Schleife an. Dieser muss am Ende jeder Schleifeniteration true ergeben, damit eine neue Schleife gestartet wird. Die IncrementStatement-Eigenschaft gibt die am Ende jeder Schleifeniteration auszuführende Anweisung an. Die Statements-Eigenschaft gibt die innerhalb einer Schleife auszuführende Auflistung von Anweisungen an.

Beispiel

In diesem Beispiel wird veranschaulicht, wie mit einem CodeIterationStatement eine for-Schleife dargestellt wird.

 ' 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)
// 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 = gcnew CodeVariableDeclarationStatement( int::typeid,"testInt",gcnew CodePrimitiveExpression( (int^)0 ) );
array<CodeMethodInvokeExpression^>^writelineparams = {gcnew CodeMethodInvokeExpression( gcnew CodeVariableReferenceExpression( "testInt" ),"ToString",nullptr )};
array<CodeStatement^>^codestatements = {gcnew CodeExpressionStatement( gcnew CodeMethodInvokeExpression( gcnew CodeMethodReferenceExpression( gcnew CodeTypeReferenceExpression( "Console" ),"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.

// Each loop iteration the value of the integer is output using the Console.WriteLine method.
CodeIterationStatement^ forLoop = gcnew CodeIterationStatement( gcnew CodeAssignStatement( gcnew CodeVariableReferenceExpression( "testInt" ),gcnew CodePrimitiveExpression( 1 ) ),gcnew CodeBinaryOperatorExpression( gcnew CodeVariableReferenceExpression( "testInt" ),CodeBinaryOperatorType::LessThan,gcnew CodePrimitiveExpression( 10 ) ),gcnew CodeAssignStatement( gcnew CodeVariableReferenceExpression( "testInt" ),gcnew CodeBinaryOperatorExpression( gcnew CodeVariableReferenceExpression( "testInt" ),CodeBinaryOperatorType::Add,gcnew CodePrimitiveExpression( 1 ) ) ),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());
// 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));

Vererbungshierarchie

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

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

CodeIterationStatement-Member
System.CodeDom-Namespace