Results 1 to 8 of 8

Thread: Object Copy

  1. #1

    Thread Starter
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    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.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    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:
    1. private object obj;
    2.  
    3. private void button1_Click(object sender, System.EventArgs e)
    4.     {
    5.         object btn=this.button1;
    6.         obj=btn;
    7.         Button x =(Button) obj;
    8.         MessageBox.Show(x.Name);
    9. }
    ~
    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]

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    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]

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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....

  6. #6
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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.

  7. #7
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    u dont even need to serialize it to disk, just to a stream
    \m/\m/

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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
  •  



Click Here to Expand Forum to Full Width