|
-
Jul 21st, 2003, 12:41 AM
#1
Thread Starter
PowerPoster
Object Copy
Hello; I need to create a copy of an object, not just a reference to it. Is there anyway to do this? These were the first things I came up with:
Code:
object copy = ((ICloneable)original).Clone();
object copy = new object(original);
but alas, objects don't implement the ICloneable interface, nor do they have a copy constructor. What's a boy to do?
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jul 21st, 2003, 02:34 AM
#2
the object doesn't have values such as "Name" etc... but you can clone an object very easily , to show how an object is cloned , here's an example.
VB Code:
private object obj;
private void button1_Click(object sender, System.EventArgs e)
{
object btn=this.button1;
obj=btn;
Button x =(Button) obj;
MessageBox.Show(x.Name);
}
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jul 21st, 2003, 12:14 PM
#3
PowerPoster
That code you have isn't copying an object, it is only copying a reference to the original object.
object btn=this.button1;
object obj=btn;
Button x =(Button) obj;
MessageBox.Show(x.Name);
x.Text = "Hello";
MessageBox.Show(((Button)btn).Text);
The reason this proves it is because if I change x's text, it shouldn't affect btn object's text....but it does, which means that x is just a reference to btn.
-
Jul 21st, 2003, 01:55 PM
#4
actually it's not the button you are supposed to be looking at , it's the 2 objects ( the first one becomes a references to the button ) the second object becomes a Clone of the first object ( which is how i understand they want it to be )
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jul 21st, 2003, 02:45 PM
#5
PowerPoster
They are not cloned object, they are variables pointing to the same object.
I changed x's text property which was made from the obj variable that is supposed to be copied from the original object btn.
But when I do that, and take the btn object, it's text propery is the exact same as x's.....so that proves you are only making obj reference the same object as btn, not copying it. Think about some more....
-
Jul 21st, 2003, 07:22 PM
#6
Frenzied Member
What hellswraith is saying is correct. To clone an object, you have to implement the IClonable interface, and write your own clone method. An easy way to clone an object would be to serialize it the harddisk, then deserialize it to the new object.
-
Jul 21st, 2003, 07:41 PM
#7
yay gay
u dont even need to serialize it to disk, just to a stream
\m/  \m/
-
Jul 21st, 2003, 07:49 PM
#8
Frenzied Member
Also, you would use serialization when you need a deep copy. If you needed a shallow copy, you could just return a new copy of the object in your clone method.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|