提问人:Sengur 提问时间:2/22/2023 最后编辑:Klaus GütterSengur 更新时间:2/22/2023 访问量:31
错误 CS1612 “Figur.Standort”的返回值不是变量,因此无法更改
Error CS1612 The return value of "Figur.Standort" is not a variable and therefore cannot be changed
问:
你好,这是我在这个页面上的第一个问题,因为我的老师和 ChatGPT 都无法帮助我,他们都说他们没有发现错误。
我想做一个程序来测试骑士是否可以在不使用两次单向方格的情况下跑过棋盘上的每个方格。
现在我有问题,我的IDE(Visual Code Community)给了我(NET7.0)
错误: “CS1612”
其中说: “”Figur.Standort“的返回值不是变量,因此无法更改。”
(错误出在方法“Bewegen”中)
class program
{
static void Main()
{
//Playfield chessboard
bool[,] Spielfeld = new bool[8, 8]; ;
//Mögliche Spielzüge des Springers
Pos[] Spielzüge = new Pos[8];
Spielzüge[0] = new Pos(1, 2);
Spielzüge[1] = new Pos(1, -2);
Spielzüge[2] = new Pos(2, 1);
Spielzüge[3] = new Pos(2, -1);
Spielzüge[4] = new Pos(-1, 2);
Spielzüge[5] = new Pos(-1, -2);
Spielzüge[6] = new Pos(-2, 1);
Spielzüge[7] = new Pos(-2, -1);
//Startposition of the knight on the playfield
Pos Position = new Pos(1, 0);
//Knight
Figur Springer = new Figur(Spielzüge, Position);
//Mainloop
while (true)
{
}
}
}
class Figur
{
public Pos[] Möglichkeiten { get; set; }
public Pos Standort { get; set; }
public Figur(Pos[] möglichkeiten, Pos standort)
{
Möglichkeiten = möglichkeiten;
Standort = standort;
}
public void Bewegen(int zugIndex)
{
Standort.X += Möglichkeiten[zugIndex].X;
Standort.Y += Möglichkeiten[zugIndex].Y;
}
}
struct Pos
{
public int X { get; set; }
public int Y { get; set; }
public Pos(int x, int y)
{
X = x;
Y = y;
}
}
我期望方法“Bewegen” 使用 in Möglichkeiten[Index] 保存的 Pos 来计算新的“Standort”
答:
1赞
Klaus Gütter
2/22/2023
#1
Pos
是一个值类型(struct),因此每当您访问该属性时,您都会得到一个副本,因此无法修改原始属性的 X/Y 属性。Standort
选项 1:通过替换为 来更改为引用类型。struct Pos
class Pos
选项 2:将新 Pos 分配给 Standort:
Standort = new Pos(Standort.X + Möglichkeiten[zugIndex].X, Standort.Y + Möglichkeiten[zugIndex].Y);
评论