Monday, 19 August 2013

How to declare an array of objects C#

How to declare an array of objects C#

I'm making a 2D tile map for a game. I Have a Cell class (tiles) makes
cell objects that have 4 attributes: TopWall, BottomWall, LeftWall,
RightWall. The wall may or may not be there, so those attributes are
boolean true or false, and if they are true, a line will be drawn down
that cell wall (not allowing the player to pass through the cell). I want
to declare a (2 dimensional? As in, row, and column) array of my cell
objects called Map1 (as they will make up a game map). Then i want to set
each member of the array to having specific wall attributes. Here is what
i have:
Cell.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Map
{
public class Cell : PictureBox
{
bool LeftWall;
bool TopWall;
bool RightWall;
bool BottomWall;
public Cell(bool TopWall, bool RightWall, bool BottomWall, bool
LeftWall)
{
this.TopWall = TopWall;
this.RightWall = RightWall;
this.BottomWall = BottomWall;
this.LeftWall = LeftWall;
}
}
}
This is me starting to attempt making the array of cell objects and set
the wall attributes: Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Map
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Array of tiles
/// </summary>
//PictureBox[,] Boxes = new PictureBox[4,4];
Cell[,] Map1 = new Cell[4,4];
for (int row = 0; row < Map1.Length; row++)
{
for (int column = 0; column < length; column++)
{
Map1[i] = new Cell();
}
}
Map1[0,0] = Cell(true, false, false, true);
It is showing a lot of errors and i was wondering if anyone can see where
i'm going wrong. Thanks.

No comments:

Post a Comment