Search code examples
c#xnagridcell

How to create a grid


I would like to create a grid, with cells(I believe this is the correct term for this, is it?) Like this(take z,x,c,v,b as my "cells"):

  z|x|c|v|b|x|c|b|b
  -----------------
  z|x|c|v|b|x|c|b|b
  -----------------
  z|x|c|v|b|x|c|b|b
  -----------------
  z|x|c|v|b|x|c|b|b
  -----------------
  z|x|c|v|b|x|c|b|b
  -----------------
  z|x|c|v|b|x|c|b|b

The order of the "cells" is random, so dont worry about that, also the lines are only there to help you visualize what i mean


Solution

  • What you are looking for are Arrays

    To make a grid specificly, you need a 2D array. For this example I will use a string as the data, but you can use any class you want.

    string[,] cells = new string[width, height];
    

    You can then access and set the values of these cells like so:

    cells[x, y] = "z";
    string something = cells[x, y];