必须在声明前使用
Must be used before declaration
我正在尝试制作一款主机游戏,但我遇到了以下代码(以及未来的代码)的问题
Node n1 = new Node("You Are in a Snowy biome, you notice your Crashed Vehicle behind you.", map1, n9, n2, null, n3, null, null, "You Notice Your Crashed Vehicle, it is Beyond use now.", null);
Node n2 = new Node("You Notice a Metal Lamp Post Standing Tall, the Light is on.", map2, n8, n4, null, n1, "You Move Past the Lamp Post.", null, "You see Part of your Crashed Vehicle, it was Pretty Long.", "This was where you Crashed.");
Node n3 = new Node("You Notice a Box with a Red Plus Sign on it.", map3, n10, n1, null, null, "You Walk Towards the box and open the Lid...", ItemN3.GetItem());
Node n4 = new Node(null, map4, n7, n5, null, n2, null, null, "A huge Wall of Fire Blocks the whole of the South.", "You Move back To the Lamp Post.");
Node n5 = new Node("You See A massive hole to the East.", map5, n6, null, null, n4, "You See the hole, Around 54ft Deep.", null, "The Wall of Fire Continues.", null);
节点采用这些参数:
但是我有错误告诉我在声明它之前我不能使用 Node
,但是当我将它移到错误上方时,我在更下方的其他节点之一上得到了相同的错误。所以它只是一个无限的错误循环。
我不知道如何解决这个问题。如果可能,请有人帮忙吗?
简化您的代码,这是问题所在:
Node n1 = new Node(n2);
Node n2 = new Node(n8);
您在声明或初始化 n2 之前尝试使用它。
相反,有一个方法或 属性 来完成分配,例如
public class Node
{
public void Next(Node n) ...
}
Node n1 = new Node();
Node n2 = new Node();
n1.Next(n2);
你的问题是你遇到了先有鸡还是先有蛋的问题。我将用这个 class:
来简化你的例子
public class Node
{
public Node(Node neighbour)
{
}
}
那你就是在有效地做到这一点:
Node n1 = new Node(n2);
Node n2 = new Node(n1);
显然这是行不通的,因为对象需要先初始化才能使用。您可以创建节点,然后再分配属性:
public class Node
{
public Node Neighbour {get;set;}
}
然后像这样初始化它们:
Node n1 = new Node();
Node n2 = new Node();
n1.Neighbour = n2;
n2.Neighbour = n1;
如果您的方向始终是基本方向,您可以创建一个二维数组 (Node[,]
),然后在初始化所有节点后自动循环分配邻居。
我正在尝试制作一款主机游戏,但我遇到了以下代码(以及未来的代码)的问题
Node n1 = new Node("You Are in a Snowy biome, you notice your Crashed Vehicle behind you.", map1, n9, n2, null, n3, null, null, "You Notice Your Crashed Vehicle, it is Beyond use now.", null);
Node n2 = new Node("You Notice a Metal Lamp Post Standing Tall, the Light is on.", map2, n8, n4, null, n1, "You Move Past the Lamp Post.", null, "You see Part of your Crashed Vehicle, it was Pretty Long.", "This was where you Crashed.");
Node n3 = new Node("You Notice a Box with a Red Plus Sign on it.", map3, n10, n1, null, null, "You Walk Towards the box and open the Lid...", ItemN3.GetItem());
Node n4 = new Node(null, map4, n7, n5, null, n2, null, null, "A huge Wall of Fire Blocks the whole of the South.", "You Move back To the Lamp Post.");
Node n5 = new Node("You See A massive hole to the East.", map5, n6, null, null, n4, "You See the hole, Around 54ft Deep.", null, "The Wall of Fire Continues.", null);
节点采用这些参数:
但是我有错误告诉我在声明它之前我不能使用 Node
,但是当我将它移到错误上方时,我在更下方的其他节点之一上得到了相同的错误。所以它只是一个无限的错误循环。
我不知道如何解决这个问题。如果可能,请有人帮忙吗?
简化您的代码,这是问题所在:
Node n1 = new Node(n2);
Node n2 = new Node(n8);
您在声明或初始化 n2 之前尝试使用它。
相反,有一个方法或 属性 来完成分配,例如
public class Node
{
public void Next(Node n) ...
}
Node n1 = new Node();
Node n2 = new Node();
n1.Next(n2);
你的问题是你遇到了先有鸡还是先有蛋的问题。我将用这个 class:
来简化你的例子public class Node
{
public Node(Node neighbour)
{
}
}
那你就是在有效地做到这一点:
Node n1 = new Node(n2);
Node n2 = new Node(n1);
显然这是行不通的,因为对象需要先初始化才能使用。您可以创建节点,然后再分配属性:
public class Node
{
public Node Neighbour {get;set;}
}
然后像这样初始化它们:
Node n1 = new Node();
Node n2 = new Node();
n1.Neighbour = n2;
n2.Neighbour = n1;
如果您的方向始终是基本方向,您可以创建一个二维数组 (Node[,]
),然后在初始化所有节点后自动循环分配邻居。