2014-07-10 38 views
1

How do I to create a copy of a custom object or collection?类似的问题,但我无法得到解决方案为我工作。我有3个相关的自定义类,并有问题从一个类的实例复制到另一个的问题。我在每个类中创建了Clone方法来专门创建新实例,以避免多个变量指向同一个对象。下面是我的课复制自定义类的问题

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 

我能一个实例类复制到另一个像这样:

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 

但是,当我添加一个class_stat到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 

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 

上面的最后一行提示“对象不支持此属性或方法错误。”当我逐步完成调试时,它会在到达Clone_Stat函数末尾之后抛出它。关于我在做什么的任何想法都是错误的?

+1

我没有看到这个代码中会初始化或分配给'temp_stat'对象的任何东西。你做到了吗? –

+1

是的,'temp_stat'已经初始化,不经意间就离开了我的文章。现在加入。 – colinwurtz

回答

1

我运行这个程序:

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 

我给你做同样的错误。

为什么错误?

此集合没有默认属性,并且当您将括号括起来时,它会尝试评估该表达式,这会引发错误。

分辨率

从该行删除括号:

cat.stats.Add (stat.Clone_Stat) 

这样就结束了这个代替

cat.stats.Add stat.Clone_Stat 

我也注意到,在您的Clone_User你有错误 - 也许这是一个错字,但你没有.Clone方法,我认为这应该像这样出于同样的原因:

user.categories.Add cat.Clone_Cat 
+1

ahh vba语法让我再次!正是我所需要的,甚至发现了我还没有注意到的错字。谢谢一堆。 – colinwurtz

+0

我没有发现我使用Debug |的错字编译选项来做到这一点:) –