|
-
Jun 14th, 2004, 08:59 PM
#1
Thread Starter
Hyperactive Member
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.
-
Jun 15th, 2004, 06:11 AM
#2
PowerPoster
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:
Dim obj1 as new TextBox
obj1.Text=TextBox1.Text
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.
-
Jun 15th, 2004, 08:23 AM
#3
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.
-
Jun 15th, 2004, 08:24 AM
#4
Frenzied Member
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.
-
Jun 15th, 2004, 09:26 AM
#5
PowerPoster
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.
-
Jun 15th, 2004, 11:14 AM
#6
Thread Starter
Hyperactive Member
-
Jun 15th, 2004, 11:52 AM
#7
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|