How can I change items inside a double array?

Viewed 599

How can I change items in my double variable based on a simple condition?

Check this example:

public partial class Form1 : Form
{

    double[] vk = new double[11] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    ...
    ...

    void setDouble()
    {
        if (bunifuDropdown1.selectedIndex == 0)
        {
            double[] vk = new double[11] { 2, 4.86, 11.81, 28.68, 69.64, 169.13, 410.75, 997.55, 2422.61, 5883.49, 21000 };
        }
        if (bunifuDropdown1.selectedIndex == 1)
        {
            double[] vk = new double[11] { 2, 4.51, 10.14, 22.81, 51.31, 115.46, 259.78, 584.51, 1315.14, 2959.07, 21000 };
        }
        if (bunifuDropdown1.selectedIndex == 2)
        {
            double[] vk = new double[11] { 2, 6.86, 18.67, 47.33, 116.94, 286.01, 696.59, 1693.71, 4115.30, 9996.29, 21000 };
        }
        if (bunifuDropdown1.selectedIndex == 3)
        {
            double[] vk = new double[11] { 2, 6.51, 16.64, 39.43, 90.72, 206.12, 465.78, 1049.99, 2364.49, 5322.09, 21000 };
        }
        if (bunifuDropdown1.selectedIndex == 4)
        {
            double[] vk = new double[11] { 2, 6.86, 18.67, 47.33, 108.94, 264.58, 642.55, 1560.47, 3789.71, 9203.58, 21000 };
        }
        if (bunifuDropdown1.selectedIndex == 5)
        {
            double[] vk = new double[11] { 2, 6.51, 16.64, 39.43, 82.72, 186.12, 418.78, 942.24, 2120.05, 4770.11, 21000 };
        }
    }

After executing the function setDouble(), these values didn't change. No errors found. How can I fix this?

2 Answers

You are re-declaring the vk in the function, which shadows the original one.
Try this:

public partial class Form1 : Form
{
    double[] vk = new double[11] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    ...
    ...

    void setDouble()
    {
        if (bunifuDropdown1.selectedIndex == 0)
        {
            // NOTE: Here instead of "double[] vk = ..." we have "vk = ..."
            vk = new double[11] { 2, 4.86, 11.81, 28.68, 69.64, 169.13, 410.75, 997.55, 2422.61, 5883.49, 21000 };
        }
        if (bunifuDropdown1.selectedIndex == 1)
        {
            vk = new double[11] { 2, 4.51, 10.14, 22.81, 51.31, 115.46, 259.78, 584.51, 1315.14, 2959.07, 21000 };
        }
        if (bunifuDropdown1.selectedIndex == 2)
        {
            vk = new double[11] { 2, 6.86, 18.67, 47.33, 116.94, 286.01, 696.59, 1693.71, 4115.30, 9996.29, 21000 };
        }
        if (bunifuDropdown1.selectedIndex == 3)
        {
            vk = new double[11] { 2, 6.51, 16.64, 39.43, 90.72, 206.12, 465.78, 1049.99, 2364.49, 5322.09, 21000 };
        }
        if (bunifuDropdown1.selectedIndex == 4)
        {
            vk = new double[11] { 2, 6.86, 18.67, 47.33, 108.94, 264.58, 642.55, 1560.47, 3789.71, 9203.58, 21000 };
        }
        if (bunifuDropdown1.selectedIndex == 5)
        {
            vk = new double[11] { 2, 6.51, 16.64, 39.43, 82.72, 186.12, 418.78, 942.24, 2120.05, 4770.11, 21000 };
        }
    }

In your current code you declare vk local variable which will disappear on leaving its scope:

void setDouble()
{
    if (bunifuDropdown1.selectedIndex == 0)
    {
        // Local variable...
        double[] vk = 
    }   // which will disappear here (on leaving its scope)

    ... 
    if (bunifuDropdown1.selectedIndex == 2)
    {
        // Yet another local variable...
        double[] vk = 
    }   // which will disappear here (on leaving its scope)
    ...
} 

Please, note, that huge setDouble function makes this kind of error easy to make and hard to find out.

Let's separate the data itself and business logic and we'll have an easy to read code like this

   public partial class Form1 : Form {
      private static readonly double[][] s_AllVks = new double[][] {
        new double[] { 2, 4.86, 11.81, 28.68,  69.64, 169.13, 410.75,  997.55, 2422.61, 5883.49, 21000 },
        new double[] { 2, 4.51, 10.14, 22.81,  51.31, 115.46, 259.78,  584.51, 1315.14, 2959.07, 21000 }, 
        new double[] { 2, 6.86, 18.67, 47.33, 116.94, 286.01, 696.59, 1693.71, 4115.30, 9996.29, 21000 }, 
        new double[] { 2, 6.86, 18.67, 47.33, 108.94, 264.58, 642.55, 1560.47, 3789.71, 9203.58, 21000 },
        new double[] { 2, 6.51, 16.64, 39.43,  82.72, 186.12, 418.78,  942.24, 2120.05, 4770.11, 21000 }, 
      }

      double[] vk = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 

      void setDouble() {
        if (bunifuDropdown1.selectedIndex >= 0 && bunifuDropdown1.selectedIndex < s_AllVks.Length)
          vk = s_AllVks[bunifuDropdown1.selectedIndex] 
      } 
Related