tags:

views:

268

answers:

1

Using CodeDom in C#, I am trying to generate the following for loop:

for (int i = 0; i < ds.Tables[0].Rows.Count; i = (i + 1))

Except that my code is generating this:

for (int i; (i < ds.Tables[0].Rows.Count); i = (i + 1))

Note that this does not initialize i to zero, which does not compile in C#. (VB does accept this).
So I have to go in later after the code is generated and fix it manually, which is mostly just annoying, but I'd like to fix it. The code to generate the entire statement is as follows:

CodeVariableDeclarationStatement idx = new CodeVariableDeclarationStatement(new CodeTypeReference("System.Int32"), "i", new CodePrimitiveExpression(0));
        CodeIndexerExpression dsIndex = new CodeIndexerExpression(new CodeVariableReferenceExpression("ds.Tables"), new CodeExpression[] { new CodePrimitiveExpression(0) });
        CodeBinaryOperatorExpression comp = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.LessThan, new CodePropertyReferenceExpression(dsIndex, "Rows.Count"));
        CodeAssignStatement incr = new CodeAssignStatement(new CodeVariableReferenceExpression("i"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1)));
        CodeIterationStatement iterator = new CodeIterationStatement(idx, comp, incr);

According to MSDN this is the way to initialize a value, unless there's something subtle I'm missing. Can anybody help?

Edit: The code is correct. It turns out another project in the solution was referencing a Redgate-SQL library that had an expired license, and it was somehow preventing my updates from getting deployed correctly. Thanks for your help and time.

+1  A: 

It seems to work well on my machine.

Returns:

for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1)) {
}

What version of the framework are you using?

configurator
This works for me as well, using .NET 3.5SP1.
Andy