C# - 对象变量赋值失败

时间:2014-07-16 20:54:06

标签: c# variables variable-assignment

我在理解为什么我的变量有一个空值时遇到了一些麻烦。

这是我的类构造函数:

private GraphNode current;
private GraphNode goal;
private GraphNode start;

private List<GraphNode> path;
private List<GraphNode> origin;

public Graph()
{
    current = new GraphNode(0, 0);
    goal = new GraphNode(0, 0);
    start = new GraphNode(0, 0);
    path = new List<GraphNode>();
    origin = new List<GraphNode>();
}

方法SetPathing的定义:

public void SetPathing(int mouseX, int mouseY)
{
    if (Contains((int)mouseX / 32, (int)mouseY / 32))
    {
        goal = GetNode((int)mouseX / 32, (int)mouseY / 32);
        current = goal;
        path.Add(current);

        while ((current.X != start.X) && (current.X != start.X))
        {
            current = origin.Where(keyValuePair => (keyValuePair.Key.X == current.X) && (keyValuePair.Key.X == current.Y)).Select(keyValuePair => keyValuePair.Value).LastOrDefault();
            path.Add(current);
        }
    }
}

当我在SetPathing中打开while循环时,我在本地屏幕中看到以下信息:

current     null                    PathFinding.GraphNode
goal        {PathFinding.GraphNode} PathFinding.GraphNode
        X   0   int
        x   0   int
        Y   5   int
        y   5   int

目标的参考值明确指定为当前后如何实现?

我可能在这里错过了一些愚蠢的东西,但是在找了两个小时之后我还没有找到它。这里没有异步。

编辑:我不想对我最初的问题过分冗长,这里有一些额外的细节,我的道歉。以下是Contains,GetNode和GetNodeIndex的详细信息。没什么好看的,我是个业余爱好者。

    public int GetNodeIndex(int x, int y)
    {
        // Find the node index in the list based on x and y
        // Return -1 if not found
        return nodes.FindIndex(n => (n.X == x) && (n.Y == y));
    }

    public GraphNode GetNode(int x, int y)
    {
        // Find the node in the list based on x and y
        // Return null if not in list
        int nodeIndex = GetNodeIndex(x, y);
        if (nodeIndex != -1)
        {
            return nodes[nodeIndex];
        }
        return null;
    }

    public bool Contains(int x, int y)
    {
        // Check if the returned value is not -1
        if (GetNodeIndex(x, y) != -1)
        {
            return true;
        }
    }

用例就像以下几行一样:

using System;

namespace Graphs
{
    class Program 
    {
        static void Main() 
        {
            Graph graph = new Graph(20, 20);
            graph.SetPathing(Mouse.GetState().X, Mouse.GetState().Y);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

让我们在这里剖析您的代码。

goal = GetNode((int)mouseX / 32, (int)mouseY / 32);

在这里,您已将变量目标设置为指向GraphNode对象。因此,为了简单起见,我们将其称为A.此时,您的值看起来像这样:

goal -> A
current -> null

接下来,你这样做:

current = goal;

现在,您的价值观如下:

goal -> A
current -> A

稍后在你的while循环中,你可以这样称呼:

current = origin.Where(keyValuePair => (keyValuePair.Key.X == current.X) && (keyValuePair.Key.X == current.Y)).Select(keyValuePair => keyValuePair.Value).LastOrDefault();

为了简单起见,我们将调用where where B的结果。所以,现在你的值看起来像这样:

goal -> A
current -> B

您在此处所做的只是更改current以指向新值。

答案 1 :(得分:0)

您展示了默认Graph构造函数的代码,

public Graph()

不是正在使用的那个。

Graph graph = new Graph(20, 20);

它们之间有什么区别吗?

另外,您确定您的代码与程序集同步吗?即根据你的描述,似乎你的断点就在这一行

while ((current.X != start.X) && (current.X != start.X))

你确定在调试时,它确实只是在调试器停止时执行到这里,或者它已经执行了两行的过滤代码了吗?

答案 2 :(得分:0)

我不确定这是怎么可能的,但答案似乎是这实际上不是While循环的第一次迭代。

我将断点设置为循环,上面发布的结果来自该断点的第一个击中。但是,在添加一个充当迭代计数器的变量之后,事实证明这实际上不是第一次迭代,而是第三次迭代。在发现之后,我继续改进while循环中的代码,以防止将当前设置为null。

我不确定这是怎么发生的。我正在使用Visual Studio 2013 Express for Desktop。从来没有遇到过这样的事情。

相关问题