CodeIterationStatement Classe
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Représente une instruction, ou une for boucle dans un bloc d’instructions, à l’aide d’une expression de test comme condition pour continuer à boucler.
public ref class CodeIterationStatement : System::CodeDom::CodeStatement
public class CodeIterationStatement : System.CodeDom.CodeStatement
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CodeIterationStatement : System.CodeDom.CodeStatement
type CodeIterationStatement = class
inherit CodeStatement
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type CodeIterationStatement = class
inherit CodeStatement
Public Class CodeIterationStatement
Inherits CodeStatement
- Héritage
- Attributs
Exemples
Cet exemple illustre l’utilisation d’une CodeIterationStatement boucle pour représenter une for boucle.
// 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.
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)
Remarques
Un CodeIterationStatement peut représenter une boucle ou while une for boucle.
La InitStatement propriété spécifie l’instruction à exécuter avant la première itération de boucle. La TestExpression propriété spécifie l’expression de continuation de boucle, qui doit être évaluée à true la fin de chaque itération de boucle pour qu’une autre itération commence. La IncrementStatement propriété spécifie l’instruction à exécuter à la fin de chaque itération de boucle. La Statements propriété spécifie la collection d’instructions à exécuter dans la boucle.
Constructeurs
| Nom | Description |
|---|---|
| CodeIterationStatement() |
Initialise une nouvelle instance de la classe CodeIterationStatement. |
| CodeIterationStatement(CodeStatement, CodeExpression, CodeStatement, CodeStatement[]) |
Initialise une nouvelle instance de la classe à l’aide CodeIterationStatement des paramètres spécifiés. |
Propriétés
| Nom | Description |
|---|---|
| EndDirectives |
Obtient un CodeDirectiveCollection objet qui contient des directives de fin. (Hérité de CodeStatement) |
| IncrementStatement |
Obtient ou définit l’instruction appelée après chaque cycle de boucle. |
| InitStatement |
Obtient ou définit l’instruction d’initialisation de boucle. |
| LinePragma |
Obtient ou définit la ligne sur laquelle l’instruction de code se produit. (Hérité de CodeStatement) |
| StartDirectives |
Obtient un CodeDirectiveCollection objet qui contient des directives de début. (Hérité de CodeStatement) |
| Statements |
Obtient la collection d’instructions à exécuter dans la boucle. |
| TestExpression |
Obtient ou définit l’expression à tester comme condition qui continue la boucle. |
| UserData |
Obtient les données définissables par l’utilisateur pour l’objet actuel. (Hérité de CodeObject) |
Méthodes
| Nom | Description |
|---|---|
| Equals(Object) |
Détermine si l’objet spécifié est égal à l’objet actuel. (Hérité de Object) |
| GetHashCode() |
Sert de fonction de hachage par défaut. (Hérité de Object) |
| GetType() |
Obtient la Type de l’instance actuelle. (Hérité de Object) |
| MemberwiseClone() |
Crée une copie superficielle du Objectactuel. (Hérité de Object) |
| ToString() |
Retourne une chaîne qui représente l’objet actuel. (Hérité de Object) |