|
-
Sep 29th, 2000, 07:12 AM
#1
Thread Starter
Junior Member
how do i copy an object that having the same value, properties from other object?
the object is come from the same class module.
the other copy of object is needed for manipulation without interference to original object.
thanks for all your info
-
Sep 29th, 2000, 07:21 AM
#2
Hyperactive Member
I do not think there is anything built in
So you will have to do it yourself.
Normally I have a method called Clone within the class module which returns a new instance of the class with each of the members in the original cloned (copied) in the new instance.
If any of the members are references to other objects, you will have to decide whether you want to clone or copy the reference. If you copy it, then both the original instance and the new one will reference the same (other) object. If you need to, you might choose to create a duplicate instance of the referenced object as well, and then the new instance will be completely separate from the original..
Regards
-
Sep 29th, 2000, 07:22 AM
#3
Addicted Member
Hi,
try creating a new instance of your object.
Code:
Dim myObj as New MyObject
Hope this helps
Shaun
Web/Application Developer
VB6 Ent (SP5), Win 2000,SQL Server 2000
-
Sep 29th, 2000, 10:37 AM
#4
Frenzied Member
S@NSIS, that will not duplicate properties. That will create an unitialized copy of the object.
Try:
Set Obj2 = Obj1
Where Obj1 is the original object and Obj2 is the object that you're copying Obj1 to.
-
Sep 29th, 2000, 10:44 AM
#5
Addicted Member
You're right!
I totally misread the post (too many late night's I think)!!!
Web/Application Developer
VB6 Ent (SP5), Win 2000,SQL Server 2000
-
Sep 29th, 2000, 11:10 AM
#6
Frenzied Member
those late nights sure wack you up, don't they?
-
Sep 29th, 2000, 02:54 PM
#7
Hyperactive Member
Copy/Clone/Duplicate
S@NSIS, your code will copy the object by one sense of the word, and if it is what aliank wants then cool.
However, since he said "without interference to original object", I thin you will find that your method performs a bitwise copy of the original. This means that the two variables will bot reference the very same object.
The code below demonstrates how your method will cause the property on the original object to be changed when the copied object's property is changed.
Code:
' in a form module
Option Explicit
Private Sub Form_Load()
Dim myA As Class1
Dim myB As Class1
Set myA = New Class1
myA.A = 23
Set myB = myA
myB.A = 101
Debug.Print myA.A, myB.A
End Sub
Code:
' in a class module
Option Explicit
Private mA As Integer
Public Property Let A(val As Integer)
mA = val
End Property
Public Property Get A() As Integer
A = mA
End Property
I hope this clarifies the difference for alaink and others 
Cheers
-
Sep 29th, 2000, 10:41 PM
#8
Frenzied Member
-
Sep 30th, 2000, 01:04 AM
#9
Hyperactive Member
OOps, I was replying to the wrong person...
My previous post was meant for mlewis 
S@NSIS's suggestion would create a new (and quite separate) instance of the object. However none of the attributes would be set in the new object.
mlewis correctly pointed this out. However mlewis, your suggestion will create a second variable which will have the same address in memory as the first.
As per my previous post, this will cause you great concern if you truly wanted to copy an object and keep the two objects distinct.
Regards
-
Oct 1st, 2000, 10:25 AM
#10
Problem to be solved with the API.
Code:
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)
Sub Dimmed()
Dim a as Class1, Dim b As Class1
Set a = New Class1
a.a = 34
CopyMemory (b, a, [#length])
Debug.Print a.a & ", " & b.a
b.a = 40
Debug.Print a.a & ", " & b.a
End Sub
'Note: The code in class module should be the same as PLewis's.
-
Oct 1st, 2000, 03:11 PM
#11
Hyperactive Member
Interesting..
Escaflowne,
I have never approached the "problem" by using CopyMemory to duplicate an object. If you can elaborate on how to ensure that you have copied the entire object I'd be very interested.
If it were a var declared as an instance of a UDT, then we could simply use the Len function. However, I haven't looked for nor heard of an equivalent for object manipulation.
One this however that your example still falls short on (given that it is possible somehow). If I try to imagin the ibject looking like a UDT at runtime, then I can see your suggestion successfully copying some of the values in the object. However, for others, I see it would only copy the reference. For example, if the class had a member which was an instance of another class, or a Collection or something of that nature, then I am sure that the CopyMemory would not do the job for us.
However, since we are all learning here, I'd bery much like to know if this is indeed possible. Although it breaks certain OO rules, I'd probably use it myself if it can be shown to work reliably 
Cheers
-
Oct 2nd, 2000, 12:57 AM
#12
Thread Starter
Junior Member
Re: Interesting..
isit secured to use API in this event. but thanks anyway for all of you outside there...
-
Oct 2nd, 2000, 08:27 AM
#13
Frenzied Member
I think this works for the following reason (note the I think is in need of very much emphasis, this is from memory from a book, it may be wrong)
the first nnn bytes of an object is a pointer to the vtable (list of members) of that object. copying these 4 bytes will yield a perfect copy of the two objects, as the vtable would point to the same place, and thus the objects would use the same memory for their members.
I think the nnn above is 4, but I may be wrong. Try it and see.
NOTE: this will not work for controls, only Classes.
-
Oct 6th, 2000, 08:31 PM
#14
Mlewis: Correct!
The 4 memory...I'm not sure. You need to know the # of blocks of memory in order to use it. I'll try coming up with a DetectMemory Function, but yes, I did flip over my API knowledge.
-
Oct 7th, 2000, 02:48 AM
#15
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
|