复制自定义类的问题

时间:2014-07-10 18:17:39

标签: vba class oop excel-vba excel

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 个答案:

答案 0 :(得分: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