Stuck here trying to initialize an array (c#) using a loop. The number of rows will change depending. I need to get back two values that I am calculating earlier in the program startweek, and endweek. Lots of examples on building int arrays using loops but nothing I can find re dynamic strings and multi dim arrays.
Thanks
how do I set the values for col1 in string[,] arrayWeeks = new string[numWeeks, col1]; Is that clearer?
(Thanks for the clarification.) You can do a multidimensional initializer like so:
Or, if your array is jagged:
If you’re in a loop, I’m guessing you want jagged. And instead of initializing with values, you may want to call
arrayWeeks[x] = new string[y];where x is the row you’re adding and y is the number of elements in that row. Then you can set each value:arrayWeeks[x][i] = ...where you are setting the ith element in the row. Your initial declaration of the array would bestring[][] arrayWeeks = new string[numRows][];So, to summarize, you probably want something that looks like this:
But, obviously, within your loop.