Reputation: 723
Similar question to How do I to create a copy of a custom object or collection?, but I can't get the solution to work for me. I have 3 related custom classes and having problems copying values from one instance of a class to another. I created Clone methods in each class to specifically create new instances to avoid multiple variables simply pointing to the same object. Here are my classes
class_stat:
Public value As Double
Public name As String
Public flagged As Boolean
Public category_name As String
Public Function Clone_Stat() As class_stat
Dim stat As class_stat
Set stat = New class_stat
stat.name = Me.name
stat.category_name = Me.category_name
stat.flagged = Me.flagged
stat.value = Me.value
Set Clone_Stat = stat
End Function
class_cat:
Public name As String
Public stats As Collection
Private Sub Class_Initialize()
Set stats = New Collection
End Sub
Private Function AddStat(ByRef stat As class_stat)
stats.Add stat
End Function
Public Function Clone_Cat() As class_category
Dim cat As class_category
Set cat = New class_category
'Copy the cat name
cat.name = Me.name
Dim stat As class_stat
'Copy all the stats
For Each stat In Me.stats
cat.stats.Add (stat.Clone_Stat)
Next stat
Set Clone_Cat = cat
End Function
class_user:
Public username As String
Public name As String
Public categories As Collection
Private Sub Class_Initialize()
Set categories = New Collection
End Sub
Private Function AddCategory(ByRef category As class_category)
categories.Add category
End Function
Public Function Clone_User() As class_User
Dim user As class_User
Set user = New class_User
Dim cat As class_category
'Set values
user.name = Me.name
user.username = Me.username
'Copy over the categories
For Each cat In Me.categories
user.categories.Add (cat.Clone)
Next cat
Set Clone_User = user
End Function
I'm able to copy one instance a class to another like this:
Dim temp_cat As class_category
Set temp_cat = New class_category
Dim temp_cat1 As class_category
Set temp_cat1 = New class_category
temp_cat1.name = "cat name"
Set temp_cat = temp_cat1.Clone_Cat
temp_cat.name = "cat name" 'Resets temp_cat name without affecting temp_cat1
However when I add a class_stat to class_category and try to copy it throws an error.
Dim temp_stat1 As class_stat
Set temp_stat1 = New class_stat
Dim temp_stat As class_stat
Set temp_stat = New class_stat
Dim temp_cat as class_category
Set temp_cat = New class_category
Dim temp_cat1 As class_category
Set temp_cat1 = New class_category
'Clone some stats fine - this works
temp_stat1.name = "stat name"
Set temp_stat = temp_stat1.Clone_Stat
temp_stat1.name = "stat1 name"
'Clone a cat with no stats - works
temp_cat1.name = "cat name"
Set temp_cat = temp_cat1.Clone_Cat
temp_cat1.name = "cat1 name"
'Try and add stats to temp_cat1 and clone and then it fails
Call temp_cat1.stats.Add(temp_stat.Clone_Stat) 'this works
Dim cloned_cat As class_category
Set cloned_cat = New class_category
Set cloned_cat = temp_cat1.Clone_Cat
The last line above throws the "Object doesn't support this property or method error." When I step through the debug it throws it after getting to the end of the Clone_Stat function. Any ideas on what I'm doing wrong?
Upvotes: 1
Views: 142
Reputation: 53653
I run this procedure:
Sub TestClones()
Dim temp_cat As class_category
Set temp_cat = New class_category
Dim temp_cat1 As class_category
Set temp_cat1 = New class_category
Dim temp_stat1 As class_stat
Set temp_stat1 = New class_stat
Dim temp_stat As class_stat
Set temp_stat = New class_stat
'Clone some stats fine - this works
temp_stat1.name = "stat name"
Set temp_stat = temp_stat1.Clone_Stat
temp_stat1.name = "stat1 name"
'Clone a cat with no stats - works
temp_cat1.name = "cat name"
Set temp_cat = temp_cat1.Clone_Cat
temp_cat1.name = "cat1 name"
'Try and add stats to temp_cat1 and clone and then it fails
Call temp_cat1.stats.Add(temp_stat.Clone_Stat) 'this works
Dim cloned_cat As class_category
Set cloned_cat = New class_category
Set cloned_cat = temp_cat1.Clone_Cat()
End Sub
I get the same error you do.
Why the error?
There is no default property for this collection, and when you put the parentheses around it, it is attempting to evaluate that expression, which raises the error.
Resolution
Remove the parentheses from this line:
cat.stats.Add (stat.Clone_Stat)
So that you end up with this instead
cat.stats.Add stat.Clone_Stat
I also notice that in your Clone_User
you have a mistake -- perhaps it is a typo but you do not have a .Clone
method and I think this should be like so for the same reason:
user.categories.Add cat.Clone_Cat
Upvotes: 1