创建一个“克隆"文件.这个对象,而不是指向它 [英] Create a "clone" of this object, not point to it
问题描述
假设我有一个名为
myFirstList
然后我想创建该列表的副本,以便自己进行一些调整.所以我这样做:
And then I want to create a copy of that list so I can do some tweaks of my own. So I do this:
mySecondList = myFirstList
mySecondList.doTweaks
但是我注意到这些调整也影响了myFirstList
对象!我只希望这些调整影响第二个...
But I noticed that the tweaks also affect the myFirstList
object! I only want the tweaks to affect the second one...
然后我将要完全删除mySecondList
,所以我做mySecondList = Nothing
并且我很好,对吧?
And afterwards I will want to completely delete mySecondList
, so I do mySecondList = Nothing
and I'm good, right?
推荐答案
但是我注意到这些调整也 影响myFirstList对象!我只是 希望这些调整影响第二个 一个...
But I noticed that the tweaks also affect the myFirstList object! I only want the tweaks to affect the second one...
当然可以.这两个变量都指向内存中的相同对象.您对一个人所做的任何事情,都会对另一个人发生.
Of course it does. Both variables are pointing to the same object in memory. Anything you do to the one, happens to the other.
根据您的需要,您将需要进行深克隆或浅克隆. 本文应该可以使您更好地了解需要做什么
You're going to need to do either a deep clone, or a shallow one, depending on your requirements. This article should give you a better idea what you need to do
这篇关于创建一个“克隆"文件.这个对象,而不是指向它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!