|
-
Jul 17th, 2008, 09:48 AM
#1
[RESOLVED] Copying an object
I have an object myObj1 of some type myObj. I want to make a copy of myObj1 so I use this code:
Code:
Dim myObj2 As myObj
Set myObj2 = MyObj1
Now, if I modify myObj2 it turns out that myObj1 undergoes the same modification. In other words, the code above is not creating an independent copy of myObj1, rather it's creating a second name for the same object. Does that make sense?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jul 17th, 2008, 10:40 AM
#2
Lively Member
Re: Copying an object
try this instead to loop round all properties inside your class to populate the new instance of your class, in this call a class object called Class1. Oh and you need a reference to the typelib information dll
Code:
Dim info As TLI.InterfaceInfo
Dim member As TLI.MemberInfo
Dim cust As Class1
Set cust = New Class1
cust.cdblDCCSterlingAmount = 12
Set info = TLI.InterfaceInfoFromObject(cust)
For Each member In info.Members
Debug.Print member.Name
If member.InvokeKind = InvokeKinds.INVOKE_PROPERTYGET Then
Debug.Print member.Name & " - " & TLI.InvokeHook(cust, member.Name, INVOKE_PROPERTYGET)
End If
Next
-
Jul 17th, 2008, 10:59 AM
#3
Lively Member
Re: Copying an object
the complete code would look like
Code:
Dim info As TLI.InterfaceInfo
Dim info2 As TLI.InterfaceInfo
Dim member As TLI.MemberInfo
Dim member2 As TLI.MemberInfo
Dim cust As Class1
Dim cust2 As Class2
Set cust = New Class1
Set cust2 = New Class2
cust.cdblDCCSterlingAmount = 12
Set info = TLI.InterfaceInfoFromObject(cust)
Set info2 = TLI.InterfaceInfoFromObject(cust2)
For Each member In info.Members
If member.InvokeKind = InvokeKinds.INVOKE_PROPERTYGET Then
Call TLI.InvokeHook(cust2, member.Name, INVOKE_PROPERTYPUT, TLI.InvokeHook(cust, member.Name, INVOKE_PROPERTYGET))
'Debug.Print member.Name & " - " & TLI.InvokeHook(cust, member.Name, INVOKE_PROPERTYGET)
End If
Next
-
Jul 17th, 2008, 04:55 PM
#4
Re: Copying an object
try
Dim myObj2 As New myObj
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 17th, 2008, 07:23 PM
#5
Re: Copying an object
 Originally Posted by krtxmrtz
I have an object myObj1 of some type myObj. I want to make a copy of myObj1 so I use this code:
Code:
Dim myObj2 As myObj
Set myObj2 = MyObj1
Now, if I modify myObj2 it turns out that myObj1 undergoes the same modification. In other words, the code above is not creating an independent copy of myObj1, rather it's creating a second name for the same object. Does that make sense?
That's normal, that code is creating a reference to the same object, not a new Object.
There is no way in VB to directly clone an object, you have to create a Property Get inside the class that returns a new object with the same info that the first. So create a Property Get inside the Class, like..
Code:
Public Property Get Clone() As yourClassname
Dim lObjClone as yourClassname
Set lObjClone = new yourClassname
'Assign all the properties that need to be cloned
lObjClone.SomeProp = SomeProp
lObjClone.SomeOtherProp = SomeOtherProp
'...
Set Clone = lObjClone
End Function
Finally you can obtain your new object doing..
Code:
Dim myObj2 As yourClassname
Set myObj2 = MyObj1.Clone()
EDIT: If you don't have access to the Class code (i don't think this the case) then you'll need a more advanced solution like the one proposed by reado, in that case for using the typelib you need to add a reference to TLBINF32.DLL, you'll find this DLL in Windows\System32 folder.
Last edited by jcis; Jul 17th, 2008 at 08:12 PM.
-
Jul 17th, 2008, 09:19 PM
#6
Re: Copying an object
For a faster Clone function, follow this logic:
Code:
' SimpleClass.cls
Private m_Text As String
Private m_Visible As Boolean
Public Function Clone() As SimpleClass
Set Clone = New SimpleClass
Clone.Serialize m_Text, m_Visible
End Function
Friend Sub Serialize(ByRef Text As String, ByVal Visible As Boolean)
m_Text = Text
m_Visible = Visible
End Sub
Alternatively you can also make an ActiveX DLL, which adds a new property for the class to the properties window: Persistable. When you set this to True, you get InitProperties, ReadProperties and WriteProperties. This allows you to save the class into a PropertyBag and through that you can make a new copy of the class by placing it to a property bag and then by reading it out.
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
|