'C# Problems with double[] myArray and If statement

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?



Solution 1:[1]

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 };
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lasse V. Karlsen