简体   繁体   English

C#-对象变量分配失败

[英]C# - Object variable assignment failing

I'm having some trouble understanding why my variable has a null value. 我在理解为什么我的变量具有空值时遇到了一些麻烦。

Here's my class constructor: 这是我的类构造函数:

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

Defenition of method SetPathing: 方法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);
        }
    }
}

When I break on the start of the while-loop in SetPathing I see the following info in the locals screen: 当我在SetPathing中while循环的开始处中断时,我在本地屏幕上看到以下信息:

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

How is that possible after having clearly assigned the reference value of goal to current ? 目标的参考值明确分配给当前后,怎么办?

I'm probably missing something stupid here, but I haven't found it after looking for two hours. 我可能在这里错过了一些愚蠢的东西,但是经过两个小时的寻找,我仍然没有找到它。 No asynchronous going on here. 这里没有异步发生。

EDIT : Didn't want to be overtly verbose with my initial question, here's some extra details, my apologies. 编辑 :不想对我的最初问题过于冗长,这里有一些额外的细节,我很抱歉。 Here are the details of Contains, GetNode and GetNodeIndex. 以下是Contains,GetNode和GetNodeIndex的详细信息。 Nothing fancy, I'm an amateur. 没什么,我是一个业余爱好者。

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

The usecase is litterally just along the lines of the following: 该用例在其他方面大致如下:

using System;

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

Let's dissect your code here. 让我们在这里剖析您的代码。

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

Here, you've set the variable goal to point to a GraphNode object. 在这里,您将变量目标设置为指向GraphNode对象。 So, for the sake of simplicity, we'll call it A. At this point your values look like this: 因此,为简单起见,我们将其称为A。此时,您的值如下所示:

goal -> A
current -> null

Next, you do this: 接下来,执行此操作:

current = goal;

And now, your values look like this: 现在,您的值如下所示:

goal -> A
current -> A

Later during your while loop, you call this: 稍后在while循环中,您将其称为:

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

For the sake of simplicity, we'll call the result of this where clause B. So, now your values look like this: 为了简单起见,我们将调用where子句B的结果。因此,现在您的值如下所示:

goal -> A
current -> B

All that you have done here is change current to point to a new value. 您在此处所做的只是将current更改为指向新值。

You showed the code for the default Graph constructor, 您显示了默认Graph构造函数的代码,

public Graph()

not the one being used. 不是正在使用的那个。

Graph graph = new Graph(20, 20);

Is there any difference between them? 它们之间有什么区别吗?

Also, are you sure your code is in sync with the assembly? 另外,您确定代码与程序集同步吗? ie from your description it seems your break point is on this line 即从您的描述看来,您的断点在这条线上

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

Are you sure when you were debugging, it was indeed executing only upto here when the debugger stopped, or it could have already executed the filtering code two lines down? 您确定在调试时,它确实仅在调试器停止时才执行到此处,或者它可能已经向下两行执行了过滤代码?

I'm not sure how this is possible, but the answer appears to be that this is in fact not the first iteration of the While-loop. 我不知道这怎么可能,但是答案似乎是这实际上不是While循环的第一次迭代。

I had my break point set to the loop, and the results I posted above were from the first hit of that break point. 我将断点设置为循环,上面发布的结果来自该断点的第一击。 However, after adding a variable that acted as an iteration counter it turned out this was in fact NOT the first iteration, but the third one. 但是,在添加了充当迭代计数器的变量后,事实证明这实际上不是第一次迭代,而是第三次迭代。 Following this discovery I went on to improve the code in the while loop to prevent current being set to null. 在发现之后,我继续改进了while循环中的代码,以防止将current设置为null。

I'm not sure how this could happen. 我不确定这怎么发生。 I'm using Visual Studio 2013 Express for Desktop. 我正在使用Visual Studio 2013 Express for Desktop。 Never encountered anything like this before. 以前从未遇到过这样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM