如何创建自定义对象或集合的副本? [英] How do I to create a copy of a custom object or collection?

查看:100
本文介绍了如何创建自定义对象或集合的副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义集合 - 我们称之为 colParentA - 它包含一些名为 colChild 的集合。我想创建一个函数,创建一个新集合, colParentB ,它具有所有属性,并包含与 colParentA 。然后,用户可以修改他们需要的 colParentB 的几个属性,而不必重新定义与 colParentA



colParentB 还应包含 colChild 的新实例,是在colParentA中找到的副本。



我不能这样做吗?

  set colParentB = colParentA 

colParentB.Name =副本& colParentA.Name

因为这只是使 colParentB 指向 colParentA ,并更改 colParentA 的属性?



我很困惑感谢您提前的帮助。

解决方案

你的猜疑是正确的 - 你所分配的都是指针,所以它会只需使用不同的名称引用对象的相同实例。



您可能需要在colParent和colChild类上创建Clone函数。这样colChild.Clone可以执行成员克隆并返回具有相同属性的全新对象,colParent可以使用克隆的colChild对象创建一个新集合。



尽管如此,如果colParent或colChild的任何属性都是对象的引用,那么那些可能需要被克隆到,以避免更新不需要的值。



可能的功能将是(请注意,我假设colChild包含一些类clsContent的实例 - 需要更改):



colParent.Clone :

 公共功能克隆()作为colParent 
'开始一个新的父集合
dim parent作为colParent
set parent = new colParent

'使用原始内容的克隆填充新集合
dim child作为colChild
为Me
父项中的每个子项.Add(child.Clone)
next

set Clone = parent
结束函数
/ pre>

colChild.clone:

 公共功能克隆()作为colChild 
'开始一个新的父集合
dim child as colChild
set child = new colChild

'用原始内容的克隆填充新集合
dim内容作为clsContent
为Me
中的每个内容child.Add(content.Clone)
下一个

set克隆= child
结束函数

clsContent.Clone:

 公共功能Clone()作为clsContent 
'开始一个新的父集合
dim内容作为clsContent
set child = new clsContent

子.Property1 = me.Property1
child.Property2 = me.Property2
...

set克隆=内容
结束函数

请原谅任何错误或打字错误 - 我没有开发环境,所以我直接写入文本框! / p>

I have a custom collection - let's call it colParentA - and it contains a number of collections called colChild. I want to create a function that creates a new collection, colParentB that has all of the properties and contains the same children as colParentA. The user can then modify the few properties of colParentB that they need to, rather than having to redefine ones that are the same as colParentA.

colParentB should also contain new instances of colChild that are copies of those found in `colParentA.

I can't just do this right?

set colParentB = colParentA

colParentB.Name = "Copy of " & colParentA.Name

Because this just makes colParentB point to colParentA and changes the properties of colParentA as well right?

I'm confused. Thanks for you help in advance.

解决方案

You are correct in your suspicions - all you are assigning is pointers, so it'll just reference the same instance of the object with a different name.

You're probably going to need to create Clone functions on your colParent and colChild classes. That way colChild.Clone can do a memberwise clone and return a brand new object with the same properties, and colParent can create a new collection with the cloned colChild objects.

Be careful though, as if any of the properies of colParent or colChild are references to object then those may need to be cloned to to avoid you updating values you don't mean to.

Possible functions would be (note that I'm supposing the colChild contains a number of instances of a class clsContent - that'll need changing):

colParent.Clone:

Public Function Clone() as colParent
  'Start a new parent collection
  dim parent as colParent
  set parent = new colParent

  'Populate the new collection with clones of the originals contents
  dim child as colChild
  for each child in Me
    parent.Add(child.Clone)
  next

  set Clone = parent
End Function

colChild.clone:

Public Function Clone() as colChild
  'Start a new parent collection
  dim child as colChild
  set child = new colChild

  'Populate the new collection with clones of the originals contents
  dim content as clsContent
  for each content in Me
    child.Add(content.Clone)
  next

  set Clone = child
End Function

clsContent.Clone:

   Public Function Clone() as clsContent
      'Start a new parent collection
      dim content as clsContent
      set child = new clsContent

      child.Property1 = me.Property1
      child.Property2 = me.Property2
      ...

      set Clone = content
    End Function

Please excuse any bugs or typos - I don't have a dev env handy, so I'm writing straight into the text box!

这篇关于如何创建自定义对象或集合的副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆