Results 1 to 7 of 7

Thread: new object thats a copy of old object [resolved]

  1. #1

    Thread Starter
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409

    new object thats a copy of old object [resolved]

    im trying to get an object from a collection and use it somewhere else
    in the somewhere else that im useing it the object will be changed some. but i dont want it to change the object in the collection

    it seems like i should just be able to say somehting like

    dim obj as new object
    obj = collection(1)

    and the object in the collection wouldnt change when obj is changed but it still does

    how can i stop it from changeing
    Last edited by dogfish227; Jun 15th, 2004 at 11:15 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: new object thats a copy of old object

    Originally posted by dogfish227
    im trying to get an object from a collection and use it somewhere else
    in the somewhere else that im useing it the object will be changed some. but i dont want it to change the object in the collection

    it seems like i should just be able to say somehting like

    dim obj as new object
    obj = collection(1)

    and the object in the collection wouldnt change when obj is changed but it still does

    how can i stop it from changeing
    what you have done is NOT create a new object but a REFERENCE to an existing object. The reference and the original object are thereafter indistinguishable.

    What you need to do is to create a new instance of the same type of object and then set all it's properties to those of the original object.

    VB Code:
    1. Dim obj1 as new TextBox
    2. obj1.Text=TextBox1.Text
    3. obj1.Forecolor=TextBox1.Forecolor

    etc.

    You can then manipulate obj1 entirely independently of TextBox1. However, I can't think of any practical reason why you would want to do this, as you would normally manipulate the object's properties using variables etc.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    To make a copy of an object I usually binary serialize it to a memory stream and deserialize it back out into the new destination object. Then you have 2 of the same thing in memory under different names and totally independant of each other. I think that is what you should do.

    It is particularly good for large and complex classes. I have to do this kind of thing all the time in my apps as there is a lot of user manipulation of data. Users are given the chance to revert to a previous vesion of the data.

    I explained my strategy in this thread a while ago...

    http://www.vbforums.com/showthread.p...hreadid=292106

    Scroll down to my first reply and read on from there.
    I don't live here any more.

  4. #4
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    You can also look into cloning, especial if it is a custom object that you need to copy, you should implement the ICloneable interface and just make a Clone method.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by SeanGrebey
    You can also look into cloning, especial if it is a custom object that you need to copy, you should implement the ICloneable interface and just make a Clone method.
    Doesn't cloning result in the original object reflecting changes in the cloned object?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409

    thanks

    wosses code worked great

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: thanks

    Originally posted by dogfish227
    wosses code worked great

    Of course. He's English. We get up earlier than the Yanks.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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