在结构上创建引用
我想将对结构的引用发送到表格。然后,我需要存储参考,以便在关闭表单之前对该结构的成员进行更改。这是代码:
struct mystruct
{
public int myvar;
}
public partial class myForm : Form
{
private mystruct refonstruct;
public myForm(ref mystruct ms)
{
// refonstruct will be a copy, not a reference :-(
refonstruct = ms;
// how can I get a reference instead?
}
public myForm_FormClosing()
{
// since refonstruct is not a reference the next line will not change foostruct
refonstruct.myvar = 17;
}
}
public void foo()
{
mystruct foostruct;
foostruct.myvar = 0;
using(myForm f= new myForm(ref foostruct))f.ShowDialog();
// here foostruct, myvar will still be 0 since refonstruct is a copy not a reference
}
将结构传递为构造函数中的引用不是问题。如果我能够对其中的结构成员进行更改(Ms.Myvar = 17),它将起作用。但是我只能在关闭对话框之前应用更改。我知道,如果我可以使用班级,那都是没有问题的,但是我出于许多原因(例如现有的庞大项目,文件格式等),我不能。摘要:有没有办法将对结构的引用存储在成员变量中?谢谢。
I want to send a reference to a struct to a form. Then I need to store the reference so I can do changes to a member of that struct before the form is closed. Here's the code:
struct mystruct
{
public int myvar;
}
public partial class myForm : Form
{
private mystruct refonstruct;
public myForm(ref mystruct ms)
{
// refonstruct will be a copy, not a reference :-(
refonstruct = ms;
// how can I get a reference instead?
}
public myForm_FormClosing()
{
// since refonstruct is not a reference the next line will not change foostruct
refonstruct.myvar = 17;
}
}
public void foo()
{
mystruct foostruct;
foostruct.myvar = 0;
using(myForm f= new myForm(ref foostruct))f.ShowDialog();
// here foostruct, myvar will still be 0 since refonstruct is a copy not a reference
}
Passing a struct as refernce in the constructor is not the problem. If I would be able to do changes to the member of the struct there (ms.myvar = 17) it would work. But I can only apply the changes before closing the dialog. In case I could use a class instead all would be no problem, I know, but I for so many reasons (e.g. existing huge project, file format, etc.) I cannot. Summary: Is there a way to store a reference to a struct in a member variable? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望表单共享数据,则结构体不是正确的使用方法。结构是值类型,并存储在使用它们的上下文中。在本例中,这将是 foo() 的堆栈帧。由于
myForm
是一个类,因此它的生命周期比foo()
更长。因此,如果它可以存储引用,则该引用可能无效并引用调用堆栈上的某个随机位置,这是不允许的。最简单的选择是将
mystruct
更改为类。另一种方法是在结构周围引入一个包装器:
这样您的结构就可以是不可变的,并且在需要更改时只需替换整个结构即可。您还可以添加一个在设置值时引发的事件,让任何表单知道它是否需要更新自身以显示更新的值。
If you want forms to share data a struct is not the correct approach to use. Structs are value types, and stored in the context where they are used. This would be the stack frame of
foo()
in this case. SincemyForm
is a class, it can have a longer lifetime thanfoo()
. So if it could store a reference, that reference might be invalid and refer to some random position on the callstack, and that is just not allowed.The easiest option is to just change
mystruct
to a class.An alternative is to introduce a wrapper around the struct:
That way your struct can be immutable, and you would just replace the entire struct when it needs to change. You can also add an event that is raised when the value is set, to let any form know if it needs to update itself to show the updated value.