【发布时间】:2014-04-28 13:50:15
【问题描述】:
我想创建一个 aminator 类。但不能修改其他类中的字段值。
这是我的简化动画师课程:
public class PointMover
{
Point point;
public void Set(ref Point p)
{
point = p;
}
public void Move(int dX)
{
point.X += dX; // The point.X is modified here.
}
}
和我的主要课程:
public partial class Form1 : Form
{
PointMover pointMover = new PointMover();
Point point = new Point(0, 0);
private void Form1_Load(object sender, EventArgs e)
{
pointMover.Set(ref point);
pointMover.Move(10); // But point.X is NOT modified here.
this.Close();
}
}
这是我的问题。有谁知道如何解决它?我会很感激的。
【问题讨论】:
-
Point是class还是struct? -
@DStanley
Point是一个结构体。 -
这无法完成。您必须将
Point包装在一个类中。你可以使用不安全的代码,但我建议不要这样做,我认为这会给你带来比它解决的问题更多的问题。 -
@LasseV.Karlsen 是的,我创建了一个 MyPoint 类来替换 struct Point 并且它可以工作。非常感谢!