【发布时间】:2019-02-12 19:01:10
【问题描述】:
我想从列表中获取指向矩形的指针。但是 Rectangle 是结构,当我尝试获取指针时,我得到了副本。
Class MyClass
{
Rectangle MovingRectangle;
List<Rectangle> RectangleList;
Point StartLocation;
***some code here***
void PicBoxMouseDown(object sender, MouseEventArgs e)
{
foreach (rectangle in RectangleList)
{
if (condition)
{
MovingRectangle = rectangle;
StartLocation = e.Location;
break;
}
}
}
void PicBoxMouseMove(object sender, MouseEventArgs e)
{
MovingRectangle.Location =
new Point(movingRectangle.Location.X + (e.X - StartLocation.X),
movingRectangle.Location.Y + (e.Y - StartLocation.Y));
}
}
我想更改列表中的矩形位置,但我更改了副本的位置。
【问题讨论】:
-
我没有在代码中看到您“更改列表中的矩形位置”。但是,您更改列表中矩形位置的方法是将其从旧位置中删除并将其插入新位置。复制一个矩形(这就是它所需要的)并不比复制一个引用(如果 Rectangle 是一个类而不是一个结构,你会做什么)做更多的工作
-
您可以并且可能应该使用rectanlge 和代码中已有的一些其他字段创建一个类。永远为成长做计划!