7

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
korn
  • 109
  • 7
  • 4
    Each `double[] vk` creates a new variable called `vk` within the scope of the `if` statement. You should look at [this question](https://stackoverflow.com/questions/1196941/variable-scope-confusion-in-c-sharp) on variable scopes. – ProgrammingLlama Mar 10 '20 at 07:03
  • 7
    remove all the `double[]` in front of `double[] vk = new double[11] ...` to be `vk = new double[11]...` – TheGeneral Mar 10 '20 at 07:04
  • 1
    You are suffering from variable shadowing. – Tanveer Badar Mar 10 '20 at 07:07

2 Answers2

9

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 };
        }
    }
Just Shadow
  • 10,860
  • 6
  • 57
  • 75
  • 3
    Just Shadow some variables :) – Alexei Levenkov Mar 10 '20 at 07:08
  • Solves the problem. Just one piece of improvement: This creates a new array every time the dropdown value is changed. I'd create a static array of double arrays in advance and just set the reference by index. – Fildor Mar 10 '20 at 07:10
  • Sure! Here I've just fixed the part of the original code which caused the issue to make it understandable. But of course we can refactor the code the way that you've mentied. – Just Shadow Jul 28 '21 at 12:56
7

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] 
      } 
Fildor
  • 14,510
  • 4
  • 35
  • 67
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 2
    Note: The proposed solution assumes `vk` is never mutated in any other way. If it is, then after any call to `setDouble`, mutating `vk` (now an alias of one of the entries in `s_AllVks`) will also mutate the corresponding `s_AllVks` entry, so future `setDouble`s that set that entry preserve the mutations. The original code would start from scratch each time with an unaliased array. – ShadowRanger Mar 10 '20 at 16:07
  • 1
    @ShadowRanger OP does not state if that's the case. Either way, if it is, this code can easily be chaged, so that a copy will be set. – Fildor Mar 11 '20 at 07:33