我正在vb.net中实现自定义的Graph算法,并且遇到以下问题:

假设代码:

dim col as new collection
dim myC as new system.collections.genericList(of myClass)

dim obj1 as new myClass
dim obj2 as new myClass

myC.add(obj1)
myC.add(obj2)

dim myC2 as new system.collections.generic.list(of myClass)

myC2 = myC

col.add(myc2)

'In the next statement, the myC2 inside col collection will be decreased to contain
'only obj1, like myC. I supose this is for myC and myC2 contains only a pointer to
'objects obj1 and obj2 as well col contains pointers to myC and myC2
myC.remove(obj2)

'The problem is that I have to only copy myC to myC2, like a ByVal argument in a function,
'instead a ByRef argument, in order to mantain a copy of objects in myC2 while these
'objects should be removed from myC. How should I do it?

感谢您的帮助...

最佳答案

您可以将myC作为参数传递给myC2的构造函数:

Dim myC2 As New System.Collections.Generic.List(Of [MyClass])(myC)

这将使用与myC相同的元素初始化一个新列表。

09-28 01:42