C# 复制对象(CheckedListBox)不引用
我在 C#(ver 4.0) 中使用 Windows 窗体应用程序,
我想复制 CheckedListBox,但不作为参考。我希望 CheckedListBox 中的每个更改都不会影响我的对象,只需分配一次,然后不再引用。
以下是我的代码:
public struct SmartFilter
{
public int from, to;
public CheckedListBox cmb;
}
var temp = new SmartFilter();
temp.from = Convert.ToInt32(cbNumber2From.SelectedItem);
temp.to = Convert.ToInt32(cbNumber2To.SelectedItem);
temp.cmb = cbNumbers2;
当我到达最后一行时,
temp.cmb = cbNumbers2;
我想在 temp.cmb (CheckedListBox) 中保存一个副本,但之后窗口中的每个更改都会影响我的对象。
I work on windows form application in C#(ver 4.0)
I want to copy a CheckedListBox, but not as reference. I want that every change in the CheckedListBox should not effect my object, just assign once, then no reference.
Following is my code:
public struct SmartFilter
{
public int from, to;
public CheckedListBox cmb;
}
var temp = new SmartFilter();
temp.from = Convert.ToInt32(cbNumber2From.SelectedItem);
temp.to = Convert.ToInt32(cbNumber2To.SelectedItem);
temp.cmb = cbNumbers2;
when I get to the last line
temp.cmb = cbNumbers2;
I want to save a copy in temp.cmb
(CheckedListBox) , but after that every change in the window effects my object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上你不能 -
CheckedListBox
不可克隆。您期望它如何运作?它是一个屏幕控件...您是否希望克隆没有父窗口,并且不在屏幕上?您对
CheckedListBox
的多少状态感兴趣?也许您应该只复制该方面而不是整个CheckedListBox
。顺便说一句,可变结构几乎总是一个坏主意,公共字段也是如此。
You can't, basically -
CheckedListBox
isn't cloneable.How would you expect it to operate? It's an on-screen control... would you expect the clone to just have no parent window, and not be on-screen? How much of the state of the
CheckedListBox
are you interested in? Maybe you should be copying just that aspect instead of the wholeCheckedListBox
.As a side-note, mutable structs are almost always a bad idea, as are public fields.