I need help with the deep copying of objects in VB.net. I am aware of the fact that there is a great amount of topics dealing with that, but I was not able to adapt it to my problem. So hopefully someone can explain it to me using my code.
The problem: I have designed a class clsParameter which has one name, one unit, one value type and one value. The value can be a double or an object of type clsVectorParameter with the properties X,Y,Z. Now I want to do a deep copy of a parameter so that X,Y,Z are also copied.
Here are the two classes. The clone function below just represents a dummy. I know that it doesn't work like this but I didn't know a better way...
Public Class clsParameter
' Using the ICloneable interface
Implements ICloneable
' Variable definition
Private m_Name As String
Private m_Unit As String
Private m_Type As String
Private m_Value As Object
' Define set and get methods
Public Property Name As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Public Property Unit As String
Get
Return m_Unit
End Get
Set(ByVal value As String)
m_Unit = value
End Set
End Property
Public Property Value As Object
Get
Return m_Value
End Get
Set(ByVal value As Object)
m_Value = value
End Set
End Property
Public Property Type As String
Get
Return m_Type
End Get
Set(ByVal value As String)
m_Type = value
End Set
End Property
' Define constructor
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
m_Value = p_Value
End Sub
' Define Clone function to create independent copies of parameter instances
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim cloneParam As New clsParameter(m_Name, m_Unit, m_Value, m_Type)
Return cloneParam
End Function
End Class
and the other class:
Public Class clsVectorParameter
Implements ICloneable
' Variable definition
Private m_x As Double
Private m_y As Double
Private m_z As Double
Public Property X As Double
Get
Return m_x
End Get
Set(ByVal value As Double)
m_x = value
End Set
End Property
Public Property Y As Double
Get
Return m_y
End Get
Set(ByVal value As Double)
m_y = value
End Set
End Property
Public Property Z As Double
Get
Return m_z
End Get
Set(ByVal value As Double)
m_z = value
End Set
End Property
' Define constructor
Public Sub New(ByVal p_x As Double, ByVal p_y As Double, ByVal p_z As Double)
m_x = p_x
m_y = p_y
m_z = p_z
End Sub
' Define Clone function to create independent copies
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim cloneVecParam As New clsParameter(m_x, m_y, m_z, "Vec")
Return cloneVecParam
End Function
End Class
I use the class in this line:
Dim aNewParam As New clsParameter("Name", "Unit", New clsVectorParameter(x,y,z), "Type")
or
Dim aNewParam As New clsParameter("Name", "Unit", Double, "Type")
Later I need to create a deep copy of this aNewParam, so the x,y,z values are also independent for all parameters.
Thank you very much for your help! Best regards, Sebastian