vba 如何创建自定义对象或集合的副本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5226147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I to create a copy of a custom object or collection?
提问by cmerrell
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
.
我有一个自定义集合 - 让我们称之为colParentA
- 它包含许多名为colChild
. 我想创建一个函数来创建一个新集合,colParentB
该集合具有所有属性并包含与colParentA
. 然后,用户可以修改colParentB
他们需要的几个属性,而不必重新定义与colParentA
.
colParentB
should also contain new instances of colChild
that are copies of those found in `colParentA.
colParentB
还应包含新实例,colChild
这些实例是在 `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?
因为这只是colParentB
指出colParentA
并改变了colParentA
as的属性,对吗?
I'm confused. Thanks for you help in advance.
我糊涂了。感谢您提前提供帮助。
回答by Jon Egerton
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.
您可能需要在 colParent 和 colChild 类上创建 Clone 函数。这样 colChild.Clone 可以进行成员克隆并返回具有相同属性的全新对象,并且 colParent 可以使用克隆的 colChild 对象创建一个新集合。
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.
但是要小心,好像 colParent 或 colChild 的任何属性都是对对象的引用,那么可能需要克隆这些属性以避免更新您不想要的值。
Possible functions would be (note that I'm supposing the colChild contains a number of instances of a class clsContent - that'll need changing):
可能的功能是(请注意,我假设 colChild 包含类 clsContent 的多个实例 - 需要更改):
colParent.Clone:
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:
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:
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!
请原谅任何错误或错别字 - 我手头没有开发环境,所以我直接写到文本框中!