Create deep copy of object

This question already has an answer here:

  • Deep cloning objects 39 answers

  • Public Sub New(ByVal p_Name As String, ByVal p_Unit As String, ByVal p_Value As Object, ByVal p_Type As String)
      m_Name = p_Name
      m_Unit = p_Unit
      m_Type = p_Type
      If TypeOf (p_Value) Is Double Then
        m_Value = p_Value
      ElseIf TypeOf (p_Value) Is clsVectorParameter Then
        m_Value = p_Value.Clone()
      End If
    End Sub
    

    To keep your code in line with for example creating a clone of an XElement , do this from the constructor:

    Dim obj1 = new clsVectorParameter(1, 1, 1)
    Dim obj2 = new clsVectorParameter(obj1)
    

    So now you only need to write an overloaded constructor, and there is no need for interfaces or seperate functions.

    An overloaded constructor can be made like this: (you need to adapt this to your own class):

    Public Class Foo
    Dim x As Integer
    Dim y As Integer
    Dim z As Integer
    
    Sub New(a As Integer, b As Integer, c As Integer)
        x = a
        y = b
        z = c
    End Sub
    
    Sub New(old As Foo)
        x = old.x
        y = old.y
        z = old.z
    End Sub
    End Class
    
    链接地址: http://www.djcxy.com/p/40776.html

    上一篇: 从没有引用的表单创建对象

    下一篇: 创建对象的深层副本