1

在defineMapCellPositions() 和defineMapCellWalls() 期间,map.cols 和map.rows 从4 和5 等值更改为0,仅用于方法的逐步执行。调试器的一个步骤证实了这一点。为什么是这样?

任何帮助表示赞赏,谢谢!

整个地图类

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;

public class Map
{
    public Map()
    {
    }
    public int rows { get; set; }
    public int cols { get; set; }
    public int boardXPos { get; set; }
    public int boardYPos { get; set; }
    public int squareSize { get; set; }

    private List<List<int>> m_cellPositions = new List<List<int>>();
    public List<List<int>> cellPositions
    {
        get
        {
            return m_cellPositions;
        }
        set
        {
            m_cellPositions = value;
        }
    }

    private List<List<int>> m_cellWalls = new List<List<int>>();
    public List<List<int>> cellWalls
    {
        get
        {
            return m_cellWalls;
        }
        set
        {
            m_cellWalls = value;
        }
    }
}

MapController 类的开始

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;

public class MapController
{
    public MapController()
    {
    }

    Map map = new Map();

设置 map.cellWalls 的方法

public void defineCellWallsList()
{
    //map.cellWalls.Clear();
    for (int row = 0; row < (map.rows * map.cols); row++)
    {
        map.cellWalls.Add(new List<int> { 0, 0 });
    }
}

设置 map.cellPositions 的方法

public void defineCellPositionsList()
{
    //map.cellPositions.Clear();
    for (int row = 0; row < map.rows; row++)
    {
        for (int col = 0; col < map.cols; col++)
        {
            map.cellPositions.Add(new List<int> { col, row });
        }
    }
}
4

1 回答 1

1

Map要公开您的实例,MapController要么将其设为公共字段,要么将其放入属性中。例子:

public class MapController
{
    public MapController()
    {
    }

    //here you make it "public" so it is visible to outside classes
    public Map map = new Map();

    // the rest of your code for this class...

然后访问该实例(假设您持有控制器的一个实例)

var controller = new MapController();
controller.map.rows = 5; // now you can access that instance of map.
controller.map.rows = 123;

现在,要将 Map 注入控制器(这意味着它是在代码中的其他地方更新的,然后可以使用类似的注入过程在多个类之间共享同一个实例),您可以执行以下操作...

public class MapController
{
    //here you make it "private" cause it doesn't need to be public anymore, 
    //you also don't new it up here, you are passing in a new on during construction.
    private Map map;
    public MapController(Map map)
    {
        this.map = map
    }

    // the rest of your code for this class...

现在在你新建对象和东西的代码中......

var map = new Map();
var controller = new MapController(map);
map.rows = 5; // now you can access that instance of map.
map.rows = 123;
// and you can easily share that same instance with other classes
var otherClass = new SomeOtherClassThatNeedsTheMap(map);
于 2013-09-14T08:17:01.657 回答