Link to home
Create AccountLog in
Visual Basic Classic

Visual Basic Classic

--

Questions

--

Followers

Top Experts

Avatar of chha7429
chha7429

Clone objects in VB 6.0
Hi there,

I need to clone objects in vb 6.0. It's a deep copy; the object contains object which in turn contains objects.

What I've done is that I've created extra classes with the properties of the class that is being cloned. The classes beeing cloned have two methods, one to create and fill the extra class and one to read the properties from the extra class.

However, it doesn't work and I'm sure there must be another way, avoiding the classes... But how? Can you direct me to a good resource? Or post an example?

My sample Code:

[Class TMetaItem]

Public attr As String

Public Function Make(cloneData As TCloneMetaItem)
    attr = cloneData.attr
End Function

Public Function Clone() As TCloneMetaItem
    Dim cloneData As TCloneMetaItem
    Set cloneData = New TCloneMetaItem
   
    cloneData.attr = attr

    Set Clone = cloneData
End Function

[Class TCloneMetaItem]

Public attr As String

[Source of error]

' Iterate over Metaitems in the collection.
Dim item As TMetaItem
For Each item In cMetaSections
    Dim newItem As TMetaItem
    Set newItem = New TMetaItem
    ' Following line gives error Object doesn't support this property or method
    newItem.Make (item.Clone)
    newcMetaSections.Add newItem
Next

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Guy Hengel [angelIII / a3]Guy Hengel [angelIII / a3]🇱🇺

You are trying to implement the Cloning  process twice :-)

Either:

Class TMetaItem]

Public attr As String

Public Function Clone() As TCloneMetaItem
   Dim cloneData As TCloneMetaItem
   Set cloneData = New TCloneMetaItem
   
   cloneData.attr = attr

   Set Clone = cloneData
End Function

[Class TCloneMetaItem]

Public attr As String

[Source of error]

' Iterate over Metaitems in the collection.
Dim item As TMetaItem
For Each item In cMetaSections
   Dim newItem As TMetaItem
   Set newItem = item.Clone
   newcMetaSections.Add newItem
Next


or this:


Class TMetaItem]

Public attr As String

Public Function Clone(cloneData As TCloneMetaItem)
   attr = cloneData.attr
End Function

[Class TCloneMetaItem]

Public attr As String

[Source of error]

' Iterate over Metaitems in the collection.
Dim item As TMetaItem
For Each item In cMetaSections
   Dim newItem As TMetaItem
   Set newItem = New TMetaItem
   newItem.Clone item
   newcMetaSections.Add newItem
Next

CHeers

Avatar of chha7429chha7429

ASKER

Thanks for the quick answer.

You're right in this case, there's just a little something. In your solution, one must discard the class TCloneMetaItem and hence the code is as follows.

[Class TMetaItem]

Public attr As String

Public Function Clone() As TMetaItem
  Dim cloneData As TMetaItem
  Set cloneData = New TMetaItem
 
  cloneData.attr = attr

  Set Clone = cloneData
End Function

[Source of error]

' Iterate over Metaitems in the collection.
Dim item As TMetaItem
For Each item In cMetaSections
  Dim newItem As TMetaItem
  Set newItem = item.Clone
  newcMetaSections.Add newItem
Next

But, this only works if all the properties are public. What if a property is private, and I don't want to have a function that returns the value of the property? I'd sort of like a cleaner method of cloning.

chha7429


ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]Guy Hengel [angelIII / a3]🇱🇺

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

Visual Basic Classic

Visual Basic Classic

--

Questions

--

Followers

Top Experts

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.