Z
Zürcher See
I have a checkbox as sample, I have to create n-checkbox like that one in a
webform, so I have extended the CheckBox class and wrote a public clone
method that use the protected MemberwiseClone method.
If I look the MemberwiseClone method definiton I find the following phrase:
"
Return Value
A shallow copy of the current Object.
.....
A shallow copy creates a new instance of the same type as the original
object, and then copies the nonstatic fields of the original object. If the
field is a value type, a bit-by-bit copy of the field is performed. If the
field is a reference type, the reference is copied but the referred object
is not; ..
"
It's perfect! Precisely what I need. A bool attribute, like
CheckBox.Checked, is a value type, so I can set a different checked state
for each checkbox-clone ... but it didn't work. What's wrong?
class Class1
{
[STAThread]
static void Main(string[] args)
{
CheckBox cb=new CheckBox();
cb.Checked=false; // !!! That's realy funny, try to comment this line and
run the code again!!!
System.Web.UI.WebControls.CheckBox cb1=cb.Clone();
System.Web.UI.WebControls.CheckBox cb2=cb.Clone();
Console.WriteLine(cb.Checked);
Console.WriteLine(cb1.Checked);
Console.WriteLine(cb2.Checked);
cb1.Checked=!cb1.Checked;
Console.WriteLine();
Console.WriteLine(cb.Checked);
Console.WriteLine(cb1.Checked);
Console.WriteLine(cb2.Checked);
cb2.Checked=!cb.Checked;
Console.WriteLine();
Console.WriteLine(cb.Checked);
Console.WriteLine(cb1.Checked);
Console.WriteLine(cb2.Checked);
Console.ReadLine();
}
private class CheckBox:System.Web.UI.WebControls.CheckBox
{
public CheckBox Clone()
{
return (CheckBox)this.MemberwiseClone();
}
}
}
webform, so I have extended the CheckBox class and wrote a public clone
method that use the protected MemberwiseClone method.
If I look the MemberwiseClone method definiton I find the following phrase:
"
Return Value
A shallow copy of the current Object.
.....
A shallow copy creates a new instance of the same type as the original
object, and then copies the nonstatic fields of the original object. If the
field is a value type, a bit-by-bit copy of the field is performed. If the
field is a reference type, the reference is copied but the referred object
is not; ..
"
It's perfect! Precisely what I need. A bool attribute, like
CheckBox.Checked, is a value type, so I can set a different checked state
for each checkbox-clone ... but it didn't work. What's wrong?
class Class1
{
[STAThread]
static void Main(string[] args)
{
CheckBox cb=new CheckBox();
cb.Checked=false; // !!! That's realy funny, try to comment this line and
run the code again!!!
System.Web.UI.WebControls.CheckBox cb1=cb.Clone();
System.Web.UI.WebControls.CheckBox cb2=cb.Clone();
Console.WriteLine(cb.Checked);
Console.WriteLine(cb1.Checked);
Console.WriteLine(cb2.Checked);
cb1.Checked=!cb1.Checked;
Console.WriteLine();
Console.WriteLine(cb.Checked);
Console.WriteLine(cb1.Checked);
Console.WriteLine(cb2.Checked);
cb2.Checked=!cb.Checked;
Console.WriteLine();
Console.WriteLine(cb.Checked);
Console.WriteLine(cb1.Checked);
Console.WriteLine(cb2.Checked);
Console.ReadLine();
}
private class CheckBox:System.Web.UI.WebControls.CheckBox
{
public CheckBox Clone()
{
return (CheckBox)this.MemberwiseClone();
}
}
}