簡體   English   中英

C#-在調試器中,一旦執行到方法,則屬性設置為0。然后,它們又返回

[英]C# - In debugger, as soon as execution gets to method, attributes set to 0. Then after, they are back

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

要在您的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...

然后訪問該實例(假設您持有控制器的實例)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM