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
Re: new object thats a copy of old object
Quote:
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.