不能在此范围内声明名为“ nextjunction”的本地或参数,因为该名称在封闭的本地范围内用于定义本地

时间:2019-10-15 22:09:34

标签: c#

我正在Windows窗体应用程序中创建游戏。游戏的基本前提是用户围绕二维鸟瞰图控制汽车。我创建了一个名为junction的类,它指示2条或更多条道路在哪里交汇,用户可以根据可用选项在交汇处转弯车辆。当汽车到达路口时,将通过以下代码计算保证会到达的下一个路口。但是,我遇到一个错误,该错误不允许我将计算出的结点重新分配给变量nextjunction

  

无法在此范围内声明名为“ nextjunction”的本地或参数,因为该名称在封闭的本地范围内用于定义本地

// nextjunction.north is a class attribute of "junction" stored as a Boolean
// which determines if there is potential for the car to move north or not.
// Same goes for other directions.

// nextjunction.northjunc is a class attribute of "junction" stored as a
// junction which indicates the first junction immediately north of the 
// given junction. Same goes for other directions, 
// eg. nextjunction.southjunc indicates the first junction immediately
// south of nextjunction.

if (nextusermove == "north" && nextjunction.north==true)
{
    junction nextjunction = nextjunction.northjunc;
}
else if (nextusermove == "east" && nextjunction.east == true)
{
    junction nextjunction = nextjunction.eastjunc;
}
else if (currentmove == "down" && nextjunction.south == true)
{
    junction nextjunction = nextjunction.southjunc;
}
else if (currentmove == "west" && nextjunction.west == true)
{
    junction nextjunction = nextjunction.westjunc;
}

//the error occurs on every line of nextjunction assignment

我希望nextjunction得到正确分配。

1 个答案:

答案 0 :(得分:0)

这是一个循环引用,将无法使用...

junction nextjunction = nextjunction.northjunc;

nextjunction无法实例化,并同时从其自身的属性中设置其值。

我会考虑重命名您的属性,请注意我将nextjunction重命名为currentjunction的位置。您没有发布所有代码,所以我看不到您如何实例化已重命名为nextjunction的{​​{1}},说,尝试一下...

currentjuction