c# copy object (CheckedListBox) not reference
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 开发者_C百科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.
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 whole CheckedListBox
.
As a side-note, mutable structs are almost always a bad idea, as are public fields.
精彩评论