Question

I am trying to do an assign statement while trying to generate some c# code using codedom.

Here are the statements I wrote:

inputmethod.Name = "CaptureInput";
inputmethod.Attributes = MemberAttributes.Public | MemberAttributes.Final;
inputmethod.Parameters.Add(
    new CodeParameterDeclarationExpression(typeof(string[]), "inputs"));

inputmethod.Statements.Add(
    new CodeBinaryOperatorExpression(
            new CodePropertyReferenceExpression(
                new CodeThisReferenceExpression(), "ColA")
            , CodeBinaryOperatorType.Assign
            , new CodeArrayIndexerExpression(
            new CodeVariableReferenceExpression("inputs"),
            new CodePrimitiveExpression(0))));

When it generates I'm getting a line that goes like:

(this.ColA = input [0]);

Compiler finds fault with above line, CS0201

How to remove the bracket in the generated output?

Was it helpful?

Solution

The brackets are part of the BinaryOperatorExpression. Don't remove them.

Use the CodeAssignStatement instead of the CodeBinaryOperatorExpression.

inputmethod.Statements.Add(new CodeAssignStatement(
    new CodeVariableReferenceExpression("ColA"),
    new CodeArrayIndexerExpression(
        new CodeVariableReferenceExpression("inputs"),
        new CodePrimitiveExpression(0))));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top