C# a List inside a List: adding to it

Here is the code:

thisRow.Column.add( tempCell ); // Creating the map Columns???
Row.add( thisRow ); // Adding the Columns to the row???
These two line is what does not work but works in VS2010

The actual error is the last line of this post

Thanks for all the help in advance

#region Map Cell Class
class MapCell
{

	/******************************************
	// This Class is to contain the specific tile 
	// information such as the TIleID
	*******************************************/ 

public

	/****************************************** 
	// Constructor:
	*******************************************/ 
	#region Constructor

	MapCell( )
	{
		TileID = 0;
		Seen = false;
	}

	#endregion

	#region Properties

	int TileID
	{
		set{ tileID = value;}
		get{ return tileID; }
	}

	bool Seen
	{
		set{ seen = value;}
		get{ return seen; }
	}

	#endregion


protected

	/****************************************** 
	// Protected Data members:
	*******************************************/
	#region Data Members

	int tileID; // What type of tile is this?
	bool seen; // Has this tile been seen?
			
	#endregion

};
	#endregion

#region Map Column Class
class MapColumn
{ // Start MapColumn Class.	

	/******************************************
	// This class is to represent a column of cells.
	*******************************************/ 

	public

	/****************************************** 
	// Public Data Members:
	*******************************************/ 
		
	/* This contains a list of Columns that contain a Map Cell */
	List < MapCell > Column;

}; // End MapColumn Class.	
#endregion
	
	
public class Map : MonoBehaviour 
{
	
	List< MapColumn > Row;
	Row = new List();
				
	for ( int y = 0; y < mapHeight; y++ ) // Creating the number of rows indicated by the map height.
	{
		
	        // Initializing the temp Map Row:
	        MapColumn thisRow = new MapColumn();
	        MapCell tempCell = new MapCell();
		
		for ( int x = 0; x < mapWidth; x++ ) // Creating the number of Columns indicated by the map width.
		{
			thisRow.Column.add( tempCell ); // Creating the map Columns???????????
		}

                // This line is what does not work
	        Row.add( thisRow ); // Adding the Columns to the row???????????????
        }
	
	
}

The error is on Both adds above
List does not contain a definition for Add and no extension method Add excepting a first argument type List

The method is called Add. In C# all method names should start with a capiral letter. Furthermore it seems you’re coming from a C++ background. In C# you have to specify an acces modifier for each element. It’s not possible to declare a group of public members.

Next thing is a class is always a reference type. Therefore your MapCell and MapColumn class are reference types. You create only one instance of your MapCell class so effectively each item in your list will reference the same MapCell.

Last thing is: Is this your whole code? A class can’t contain any code. A class can only contain variable / property declarations and method declarations. Only methods or property getter / setter can contain actual code. You don’t have to include all of your code, but what you post should make sense. If you post just a fragment, you can add a comment like

// [...]
some fragment
// [...]

As final note: You shouldn’t overuse regions. They just make everything much harder to read. Also comments are in general good, but you should only add comments when necessary. Comments which just explain what’s comming next are useless since you see what is comming next. It’s just redundancy which can even lead to miss-documentation.

edit
This should work:

// C#
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MapCell
{
    public MapCell( )
    {
        TileID = 0;
        Seen = false;
    }
    
    public int TileID
    {
        set{ tileID = value;}
        get{ return tileID; }
    }
    bool Seen
    {
        set{ seen = value;}
        get{ return seen; }
    }

    protected int tileID; // What type of tile is this?
    protected bool seen; // Has this tile been seen?
}

public class MapColumn
{
    public List< MapCell > Column = new List< MapCell >();
}

public class Map : MonoBehaviour
{
    public int mapHeight;
    public int mapWidth;
    public List< MapColumn > map = new List< MapColumn >();
    void Start()
    {
        for ( int y = 0; y < mapHeight; y++ )
        {
            var column = new MapColumn();
            map.Add(column);
            for ( int x = 0; x < mapWidth; x++ )
            {
                var cell = new MapCell();
                column.Column.Add( cell );
            }
        }
    }
}

Keep in mind if you plan to edit / set the map in the Inspector, you should use public variables since properties aren’t serialized since they are actually methods. Also if you want to set / initialize the map in the Inspector you shouldn’t create the grid manually since this will erase what you set in the Inspector.