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?
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 .
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.
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.
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 };
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With