Home > Back-end >  C# Problems with double[] myArray and If statement
C# Problems with double[] myArray and If statement

Time:03-18

Hello I don't know why my code does not work, here the extract

        bool flag = true;
        double[] dblColsBis = { 60, 90, 90, 90, 90 };

        double[] dblRowsBis = { 20 };


        if (flag)
        {
             dblColsBis = { 120, 60, 60, 60, 60 };

             dblRowsBis = { 15 };
        }

Can you help me please?

CodePudding user response:

It really helps when you write what "does not work" mean, but in this case I can see what is wrong.

This syntax:

double[] dblColsBis = { 60, 90, 90, 90, 90 };

is only available when you declare the variable, in particular, the initialization syntax there.

If you want to assign a new array instance to that variable, you have to also construct the new array instance, like the following adjusted copy of your code:

bool flag = true;
double[] dblColsBis = { 60, 90, 90, 90, 90 };

double[] dblRowsBis = { 20 };


if (flag)
{
    dblColsBis = new double[] { 120, 60, 60, 60, 60 };

    dblRowsBis = new double[] { 15 };
}
  •  Tags:  
  • c#
  • Related