0

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

Seb_Ing
  • 1
  • 2
  • I wouldn't use `Clone` since it's not clear whether `Clone` does make a deep or shallow copy. MS also advises against using it. That's why it's [obsolete](https://blogs.msdn.microsoft.com/brada/2004/05/03/should-we-obsolete-icloneable-the-slar-on-system-icloneable/). Create a `Copy`/`DeepCopy` method. Apart from that it's not clear what issue you actually have. – Tim Schmelter Sep 12 '16 at 10:02
  • Put `Option Strict On` at the top of the source code file to let the compiler tell you about the mistake. – Hans Passant Sep 12 '16 at 16:22
  • Ok let us say I use a deep copy method. Can someone maybe explain me how I implement it in my specific code. I've seen many examples, but the adaptation to my problem fails all the time. – Seb_Ing Sep 15 '16 at 08:16

2 Answers2

0

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
DrDonut
  • 864
  • 14
  • 26
  • Thank you for your answer. I tried to write an overloaded constructor, but an error message appeared inside the clone function: "Overload resolution failed because no accessible 'New' can be called without a narrowing conversion" But how can this help me with my problem? – Seb_Ing Sep 15 '16 at 08:13
  • @Seb_Ing Added an example to my answer. – DrDonut Sep 15 '16 at 10:38
0
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
Fuzhou Hu
  • 528
  • 2
  • 8