Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change items inside a double array?

Tags:

arrays

c#

.net

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?

like image 543
korn Avatar asked Mar 10 '20 07:03

korn


People also ask

How do you change elements of a 2D array?

In Java, elements in a 2D array can be modified in a similar fashion to modifying elements in a 1D array. Setting arr[i][j] equal to a new value will modify the element in row i column j of the array arr .

How do you change a value in a two-dimensional array in Python?

Update elements in a 2 -D (Two Dimensional) ArrayCreate a program to update the existing value of a 2D array in Python. from array import * # import all package related to the array. arr1 = [[1, 2, 3, 4], [8, 9, 10, 12]] # initialize the array elements. print(arr1) # print the arr1 elements.

How do you change something in an array in Java?

If you want to change the size, you need to create a new array of the desired size, and then copy elements from the old array to the new array, and use the new array. In our example, arr can only hold int values. Arrays can hold primitive values, unlike ArrayList, which can only hold object values.


1 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 };
        }
    }
like image 55
Just Shadow Avatar answered Dec 02 '22 21:12

Just Shadow