简体   繁体   中英

How can I put an array in the first index of a 2D array

I basically want to put a created array that contains student grades that I took from numericUpDowns into the first index of a 2D array so if I created another student the first one's grades won't be replaced because I would place the second's one in the second index of my 2D array and call this index.

I created and initialized an array that contains 5 indexes with 1 grade for each index and I tried to give my 2D array[i][0] (with a for of course) the value of the whole grades array

static NumericUpDown[] tabGrades = new NumericUpDown[5];

NumericUpDown[][] tempGrades = new NumericUpDown[tabGrades.Length][];

...

for (int i = 0; i < tempGrades.Length; i++)

{

tempGrades[i][0] = tabGrades[];

break;

}

I expected my 2D array to simply take the array as it's first index value but instead it is telling me that I have a syntax error and that a ";" is missing

The tempGrades[i][0] represents a single element of 2D array which is of type NumericUpDown and you are trying to assign an array to that, which is an error. What you need to do is assign the array to tempGrades[i] which will represent a row(1D array) in 2D array. So you code would look like tempGrades[i][0] = tabGrades; . Your code have other issue like:

  1. Array length should be defined while initializing them so

    new NumericUpDown[tabGrades.Length][] this is an error, you have to specify the number of columns also and number of columns should be equal to the length of tabGrades.Length as only then you would be tempGrades[i] would be able to store tabGrades

  2. why you are using a loop if you need to run the 1st iteration only. You code should like:

    tempGrades[0][0] = tabGrades;

So you final code should look like:

static NumericUpDown[] tabGrades = new NumericUpDown[5];

//Here use whatever length you want instead of 10.
NumericUpDown[][] tempGrades = new NumericUpDown[10][tabGrades.Length]; 

...

tempGrades[0][0] = tabGrades;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM