不知道大家有没有试过??写段代码
dim clsA as new class1
dim colC as new conl1   'conl1 是class1的集合
dim colD as new conl1   'conl1 是class1的集合colC.add clsA,"1"
colD.add clsA,"1"问题出现了,clsA / colC.item("1") /colD.item("1") 是同一个引用
当你改变其中的一个时,其他的两个都同时改变!
解决的方法是Clone一个新的!
如:clsB=clsA.clone
    colC.add clsB
    clsC=clsA.clone
    colD.add clsC请问大家有没有什么好的方法实现Clone??
我现在是定义一个新的,然后将其中的属性一个一个赋值!!笨死了!!

解决方案 »

  1.   

    不知道大家有没有试过??写段代码
    dim clsA as new class1
    dim colC as new conl1  'conl1是class1的集合
    dim colD as& new conl1  'conl1 是class1的集合colC.add clsA,"1"
    colD.add clsA,"1"问题出现了,clsA / colC.item("1") /colD.item("1") 是同一个引用
    当你改变其中的一个时,其他的两个都同时改变!
    解决的方法是Clone一个新的!
    如:clsB=clsA.clone
        colC.add clsB
        clsC=clsA.clone
        colD.add clsC请问大家有没有什么好的方法实现Clone??
    我现在是定义一个新的,然后将其中的属性一个一个赋值!!笨死了!!
      

  2.   

    Function CloneObject(ByVal obj As Object) As Object
        ' Create a memory stream and a formatter.
        Dim ms As New System.IO.MemoryStream(1000)
        Dim bf As New BinaryFormatter(Nothing, _
            New StreamingContext(StreamingContextStates.Clone))
        ' Serialize the object into the stream.
        bf.Serialize(ms, obj)
        ' Position streem pointer back to first byte.
        ms.Seek(0, SeekOrigin.Begin)
        ' Deserialize into another object.
        CloneObject = bf.Deserialize(ms)
        ' release memory.
        ms.Close()
    End Function这段代码是用.net编写的,你试试吧
      

  3.   

    .net 我也会亚,但VB怎么序列化??
    那位教教我!!
      

  4.   

    ?Class1Dim o1 AS New Class1
    Dim o2 AS New Class1o1.Add ....Set o2=o1这样不行么?