copying objects - newbie question
-
If I set an object variable equal to another ie Dim object1 As CThing Dim object2 As CThing Set object1 = new CThing Set object2 = new CThing object1.init object2.init object1 = object2 Then obviously I now just have the one instance of the object, with two references to it (object1 and 2 just point to the same instance). My question: How do I actually copy the properties (variables etc) of one object to another? Do I have to implement a copy constructor type function i.e Sub Copy(Dest As CThing) Dest.Prop1 = Prop1 Dest.Prop2 = Prop2 '...etc End Also, I am wondering about collection objects in a similar sense. Dim List1 As Collection Dim List2 As Collection '... add objects to list1 List2 = List1 When I set one collection equal to the other, do I just have two sets of references to the collection of objects (ie change objects in list2 and list1 objects are affected) ? Hope you can help, thanks stacks in advance :)
-
If I set an object variable equal to another ie Dim object1 As CThing Dim object2 As CThing Set object1 = new CThing Set object2 = new CThing object1.init object2.init object1 = object2 Then obviously I now just have the one instance of the object, with two references to it (object1 and 2 just point to the same instance). My question: How do I actually copy the properties (variables etc) of one object to another? Do I have to implement a copy constructor type function i.e Sub Copy(Dest As CThing) Dest.Prop1 = Prop1 Dest.Prop2 = Prop2 '...etc End Also, I am wondering about collection objects in a similar sense. Dim List1 As Collection Dim List2 As Collection '... add objects to list1 List2 = List1 When I set one collection equal to the other, do I just have two sets of references to the collection of objects (ie change objects in list2 and list1 objects are affected) ? Hope you can help, thanks stacks in advance :)
AFAIK you'll have to create your own copy contstructor. If you simply do a Object1 = Object2, VB will use a default property for the object. To illustrate this point, create a blank form and place two frames on it and two command buttons. For the Command2 set the CausesValidation property to False (default is True). Run the following code...
Private Sub Form_Load() Frame1 = Frame2 Command1 = Command2 MsgBox Command1 End Sub
Notice the second frame now has the caption of the first. The first command button now has CausesValidation set to false, as shown by the message box. Hope this helps. Jeremy L. Falcon "The One Who Said, 'The One Who Said...'" -
AFAIK you'll have to create your own copy contstructor. If you simply do a Object1 = Object2, VB will use a default property for the object. To illustrate this point, create a blank form and place two frames on it and two command buttons. For the Command2 set the CausesValidation property to False (default is True). Run the following code...
Private Sub Form_Load() Frame1 = Frame2 Command1 = Command2 MsgBox Command1 End Sub
Notice the second frame now has the caption of the first. The first command button now has CausesValidation set to false, as shown by the message box. Hope this helps. Jeremy L. Falcon "The One Who Said, 'The One Who Said...'"Jeremy Falcon wrote: AFAIK you'll have to create your own copy contstructor. If you simply do a Object1 = Object2, VB will use a default property for the object. Actually VB will not do anything. There's no such thing as a deep copy in COM (which is what VB classes are). It will only do an QueryInterface() (if necessary) call and then AddRef(). ___________ Klaus [www.vbbox.com]