Array value resets automatically

Ok this silly problem is connected to the post HERE. What I did is basically removed the return and was able to set the values of the xValues array depending on combobox selection index. as per this pic enter image description here But as I try to call another method to divide certain variable with xValues.Length it gives me 'System.DivideByZeroException error as the value for xValues and xValues.Length resets to zero. Here is the code snippet:

int[] xValues = { }; //declaring empty array

private void comboBox1_SelectedValueChanged(object sender, EventArgs e) //using selection
                                                                        //to set the xValues
{
      if (comboBox1.SelectedIndex == 0)
    {
        int[]xValues= { 1, 2, 3, 4, 5 };

    }
    else if (comboBox1.SelectedIndex == 1)
    {
       int[] xValues = { 6, 7, 8, 9, 10 };

    }
    else if (comboBox1.SelectedIndex == 2)
    {
        int[] xValues = { 11, 12, 13, 14, 15 };

    }        
}

And then lets say I'm calling a method doSomeThing()

public void doSomeThing()
{
    int bSum = bValues.Sum(); //bValues comes from different input and in debugger 
                              //it shows expected values.
    int aSum = xValues.Sum(); //Here the debugger tells me aSum doesn't exists
    int slope = bSum / xValues.Length; //Divided by zero exception error goes here.
}

Why and how the the values are resetting for xValues?

Jon Skeet
people
quotationmark

waka's answer is right about what's wrong - you're declaring new local variables which are entirely independent of the instance variable.

However, waka's fix isn't quite right - you can only initialize arrays in that particular way at the point of the declaration of the variable. To assign a new value to the variable, you need slightly different syntax:

xValues = new[] { 1, 2, 3, 4, 5 };

Or if you want to specify the element type explicitly:

xValues = new int[] { 1, 2, 3, 4, 5 };

people

See more on this question at Stackoverflow