Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-在调试器中,一旦执行到方法,属性设置为0。然后,他们回来了_C#_List - Fatal编程技术网

C#-在调试器中,一旦执行到方法,属性设置为0。然后,他们回来了

C#-在调试器中,一旦执行到方法,属性设置为0。然后,他们回来了,c#,list,C#,List,在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

在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;
        }
    }
}
方法来设置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 });
    }
}
public void defineCellWallsList()
{
//map.cellWalls.Clear();
对于(int row=0;row<(map.rows*map.cols);row++)
{
添加(新列表{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 });
        }
    }
}
public void defineCellPositionsList()
{
//map.cellPositions.Clear();
for(int row=0;row
要在
MapController
中公开
Map
的实例,请将其设置为公共字段,或将其放置在属性中。例如:

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...
然后访问该实例(假设您持有控制器的实例)

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

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);

要在
MapController
中公开
Map
的实例,请将其设置为公共字段,或将其置于属性中。例如:

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...
然后访问该实例(假设您持有控制器的实例)

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

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);


map.cols和map.rows在运行defineCellPositionsList()和defineCellWallsList()时将在短时间内设置为0。由我来找出原因。你在哪里将行/列设置为4和5?
map.CellPositions
是可观察的集合吗?当您向其中添加项目时,是否会触发?@Corak行和列由用户在表单中输入设置。我现在将显示该部分。@Corak I硬编码行和列,并循环生成两个锯齿列表,例如,它们的计数为25,然后在该方法完成后,计数为0map。在运行defineCellPositionsList()和defineCellWallsList()时,cols和map.rows在短时间内被设置为0。由我来找出原因。你在哪里将行/列设置为4和5?
map.CellPositions
是可观察的集合吗?当您向其中添加项目时,是否会触发?@Corak行和列由用户在表单中输入设置。我现在将显示该部分。@Corak I硬编码行和列,并循环生成两个锯齿状列表,例如,它们的计数为25,然后在方法完成后,计数为0!如何将map实例设置为其他类中的变量作为该实例,这样就不必每次都编写控制器?它被引用了很多。例如Map=controller.Map();这实际上取决于代码结构,但有几种方法可以做到这一点。在我看来,您基本上希望在代码中的所有位置都使用相同的映射。我建议不要在控制器中创建它,而是将它注入控制器中。我将更新我的答案,向您展示它是如何工作的。但是,如果您确实希望将构造留在控制器中,然后将该实例捕获到其他地方的变量中以便于使用,您只需执行以下操作
var map=controller.map(假设您在控制器上留下了公开的
map
代码)谢谢。它不喜欢“私人地图”;但我发现你只是为了得到这个类型,然后做了私人地图;。它似乎不喜欢“var”,不知道该怎么办我想我计划在任何方法之外定义map和controller,这样我就可以在整个类中使用它,而var不适用于此谢谢!如何将map实例设置为其他类中的变量作为该实例,这样就不必每次都编写控制器?它被引用了很多。例如Map=controller.Map();这实际上取决于代码结构,但有几种方法可以做到这一点。在我看来,您基本上希望在代码中的所有位置都使用相同的映射。我建议不要在控制器中创建它,而是将它注入控制器中。我将更新我的答案,向您展示它是如何工作的。但是,如果您确实希望将构造留在控制器中,然后将该实例捕获到其他地方的变量中以便于使用,您只需执行以下操作
var map=controller.map(假设您在控制器上留下了公开的
map
代码)谢谢。它不喜欢“私人地图”;但我发现你只是为了得到这个类型,然后做了私人地图;。它似乎不喜欢“var”,不知道该怎么办我想我计划在任何方法之外定义map和controller,这样我就可以在整个类中使用它,而var对此不起作用