'Dynamic 2D & 3D arrays C#?
I've discovered I can specify the dimensions of an array using static ints like so:
static int VertRows = 3;
static int HorRows = 3;
int[,] gameBoard = new int[VertRows, HorRows];
However, I'm assuming that because they are static I will not be able to alter VertRows or HorRows during runtime? Is there a way to specify them when the script starts, or while it's running?
Solution 1:[1]
If you want to change the value of VertRows and HorRows on startup, you can refer to this code:
class Program
{
static int VertRows = 3;
static int HorRows = 3;
int[,] gameBoard = new int[VertRows, HorRows];
static void Main(string[] args)
{
Console.WriteLine("Enter New VertRows");
VertRows=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter New HorRows");
HorRows=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("VertRows is "+VertRows);
Console.WriteLine("HorRows is "+HorRows);
Console.ReadLine();
}
}
Refer to 1 and 2 we can see that constants are declared with the const modifier and cannot be changed for the life of the program. But the fields declared by the static modifier can be changed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
