C# 传递静态值
所以我有一个int x,我需要传递给另一个函数,问题是在我使用这个函数之前x发生了变化,所以x的值变为一个不同于我想要的值。有没有办法复制一个整数(我找不到)?那会解决我的问题C# 传递静态值,c#,C#,所以我有一个int x,我需要传递给另一个函数,问题是在我使用这个函数之前x发生了变化,所以x的值变为一个不同于我想要的值。有没有办法复制一个整数(我找不到)?那会解决我的问题 private void Init() { for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { Button tmpButton = new Button();
private void Init()
{
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
Button tmpButton = new Button();
tmpButton.Click += (sender, e) => ButtonClick(sender, e, x, y);
//x and y need to "freeze" their value at this point
}
}
}
private void ButtonClick(object sender, EventArgs e, int x, int y)
{
Console.Out.WriteLine(x.ToString() + ":" y.ToString());
}
private void Init()
{
对于(int x=0;x<10;x++)
{
对于(int y=0;y<10;y++)
{
按钮tmpButton=新按钮();
TMP按钮。单击+=(发送者,e)=>按钮单击(发送者,e,x,y);
//此时x和y需要“冻结”其值
}
}
}
私有无效按钮单击(对象发送者、事件参数e、整数x、整数y)
{
Console.Out.WriteLine(x.ToString()+“:“y.ToString());
}
输出:“10:10”
预期输出(如果单击按钮3,4):“3:4”您应该使用一个临时变量:
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
int tempX = x;
int tempY = y;
Button tmpButton = new Button();
tmpButton.Click += (sender, e) => ButtonClick(sender, e, tempX, tempY);
//x and y need to "freeze" their value at this point
}
}
for(int x=0;x<10;x++)
{
对于(int y=0;y<10;y++)
{
int tempX=x;
int tempY=y;
按钮tmpButton=新按钮();
TMP按钮。单击+=(发送者,e)=>按钮单击(发送者,e,tempX,tempY);
//此时x和y需要“冻结”其值
}
}
在循环中捕获变量时,应始终小心
基本上,您的lambda捕获的是变量,而不是变量的值。所以当按下按钮时,循环当然结束了,变量的值是10
Eric Lippert已经写了一篇关于为什么会发生这种情况的文章。您应该使用一个临时变量:
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
int tempX = x;
int tempY = y;
Button tmpButton = new Button();
tmpButton.Click += (sender, e) => ButtonClick(sender, e, tempX, tempY);
//x and y need to "freeze" their value at this point
}
}
for(int x=0;x<10;x++)
{
对于(int y=0;y<10;y++)
{
int tempX=x;
int tempY=y;
按钮tmpButton=新按钮();
TMP按钮。单击+=(发送者,e)=>按钮单击(发送者,e,tempX,tempY);
//此时x和y需要“冻结”其值
}
}
在循环中捕获变量时,应始终小心
基本上,您的lambda捕获的是变量,而不是变量的值。所以当按下按钮时,循环当然结束了,变量的值是10
Eric Lippert已经写了一篇关于为什么会发生这种情况的文章。您应该使用一个临时变量:
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
int tempX = x;
int tempY = y;
Button tmpButton = new Button();
tmpButton.Click += (sender, e) => ButtonClick(sender, e, tempX, tempY);
//x and y need to "freeze" their value at this point
}
}
for(int x=0;x<10;x++)
{
对于(int y=0;y<10;y++)
{
int tempX=x;
int tempY=y;
按钮tmpButton=新按钮();
TMP按钮。单击+=(发送者,e)=>按钮单击(发送者,e,tempX,tempY);
//此时x和y需要“冻结”其值
}
}
在循环中捕获变量时,应始终小心
基本上,您的lambda捕获的是变量,而不是变量的值。所以当按下按钮时,循环当然结束了,变量的值是10
Eric Lippert已经写了一篇关于为什么会发生这种情况的文章。您应该使用一个临时变量:
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
int tempX = x;
int tempY = y;
Button tmpButton = new Button();
tmpButton.Click += (sender, e) => ButtonClick(sender, e, tempX, tempY);
//x and y need to "freeze" their value at this point
}
}
for(int x=0;x<10;x++)
{
对于(int y=0;y<10;y++)
{
int tempX=x;
int tempY=y;
按钮tmpButton=新按钮();
TMP按钮。单击+=(发送者,e)=>按钮单击(发送者,e,tempX,tempY);
//此时x和y需要“冻结”其值
}
}
在循环中捕获变量时,应始终小心
基本上,您的lambda捕获的是变量,而不是变量的值。所以当按下按钮时,循环当然结束了,变量的值是10
Eric Lippert写了一篇关于为什么会发生这种情况的文章。这是一个闭包问题,可以通过使用一个临时变量来解决
int localX = x;
int localY = y;
要更好地理解捕获的变量,请参见这是一个闭包问题,可以通过使用临时变量来解决
int localX = x;
int localY = y;
要更好地理解捕获的变量,请参见这是一个闭包问题,可以通过使用临时变量来解决
int localX = x;
int localY = y;
要更好地理解捕获的变量,请参见这是一个闭包问题,可以通过使用临时变量来解决
int localX = x;
int localY = y;
要更好地理解捕获的变量,请参见您可以简单地将值存储在标记中,然后在eventhandler中检索它们。好主意,但是如何在标记中存储两个int值?我可以将矩阵转换为线性(例如:10*x+y),但我真的不想将它转换回来。正如@JasonSec所说,您需要将它们放在每个按钮的
标记中。可以使用点
或其他自定义的结构
来封装它们,并将它们作为一件事放在标记中。我想使用int数组,但Point在这种情况下效果很好。很好的建议:)您可以简单地将值存储在标记中,然后在eventhandler中检索它们。好主意,但是如何在标记中存储两个int值呢?我可以将矩阵转换为线性(例如:10*x+y),但我真的不想将它转换回来。正如@JasonSec所说,您需要将它们放在每个按钮的标记中。可以使用点
或其他自定义的结构
来封装它们,并将它们作为一件事放在标记中。我想使用int数组,但Point在这种情况下效果很好。很好的建议:)您可以简单地将值存储在标记中,然后在eventhandler中检索它们。好主意,但是如何在标记中存储两个int值呢?我可以将矩阵转换为线性(例如:10*x+y),但我真的不想将它转换回来。正如@JasonSec所说,您需要将它们放在每个按钮的标记中。可以使用点
或其他自定义的结构
来封装它们,并将它们作为一件事放在标记中。我想使用int数组,但Point在这种情况下效果很好。很好的建议:)您可以简单地将值存储在标记中,然后在eventhandler中检索它们。好主意,但是如何在标记中存储两个int值呢?我可以转换成t