Help With ICloneable And Deep Copy

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi all. I'm trying to implement ICloneable with the intent of making
a completely new copy of the example class car below. I've read lots
of posts on this and other boards on the topic but I just can't quite
seem to get the code correct in the Clone function. I want a deep
copy so that in my example code, I can change the properties of NewCar
without affecting the properties of OldCar. All of my attempts end up
with a shallow copy where changing NewCar also changes OldCar. Not
sure what I'm doing wrong. Any help would be greatly appreciated.


Module Module1
Sub Main()

Dim OldCar As New Car

OldCar.TirePressure(0) = 11.0
OldCar.TirePressure(1) = 12.0
OldCar.TirePressure(2) = 13.0
OldCar.TirePressure(3) = 14.0
OldCar.Color = "Green"

Dim NewCar As Car = OldCar

NewCar.TirePressure(0) = 26.0
NewCar.TirePressure(1) = 27.0
NewCar.TirePressure(2) = 28.0
NewCar.TirePressure(3) = 29.0
NewCar.Color = "Blue"

Console.WriteLine("Old Car Tire Pressures: " & _
OldCar.TirePressure(0).ToString("0.0") & ", " & _
OldCar.TirePressure(1).ToString("0.0") & ", " & _
OldCar.TirePressure(2).ToString("0.0") & ", " & _
OldCar.TirePressure(3).ToString("0.0"))
Console.WriteLine("Old Car Color: " & OldCar.Color)

Console.WriteLine(vbCr)

Console.WriteLine("New Car Tire Pressures: " & _
NewCar.TirePressure(0).ToString("0.0") & ", " & _
NewCar.TirePressure(1).ToString("0.0") & ", " & _
NewCar.TirePressure(2).ToString("0.0") & ", " & _
NewCar.TirePressure(3).ToString("0.0"))
Console.WriteLine("New Car Color: " & NewCar.Color)

Console.WriteLine(vbCr)

Console.Write("Press any key to continue...")
Console.ReadLine()

End Sub
End Module

Class Car
Implements ICloneable

Public Function Clone() As Object Implements ICloneable.Clone
Dim oClone As New Car


'-----------------------------------------------------------------------------------------------------
'WHAT DO I PUT HERE EXACTLY????????????????????????

'-----------------------------------------------------------------------------------------------------

Return oClone
End Function

Private _TirePressure(3) As Single
Public Property TirePressure(ByVal Index As Integer) As Single
Get
Return _TirePressure(Index)
End Get
Set(ByVal value As Single)
_TirePressure(Index) = value
End Set
End Property

Private _Color As String
Public Property Color() As String
Get
Return _Color
End Get
Set(ByVal value As String)
_Color = value
End Set
End Property

End Class
.



Relevant Pages

  • Re: Aggregation vs composition
    ... >> class Car { ... >destroying Engine, in such a way that everything is the same as before. ... >responsibility for the part. ...
    (comp.object)
  • Re: Help With ICloneable And Deep Copy
    ... a completely new copy of the example class car below. ... with a shallow copy where changing NewCar also changes OldCar. ... Dim OldCar As New Car ... Dim oClone As New Car ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Help With ICloneable And Deep Copy
    ... a completely new copy of the example class car below. ... Public Function Clone() As Object Implements ICloneable.Clone ... Dim oClone As New Car ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Checking objects static or not
    ... > - extends JFrame ... > game" button, if yes, declare a Jframe and initialize it with the Game ... For example thinking of cars, you might have a class Car. ...
    (comp.lang.java.programmer)
  • Re: About absolute reference frame......
    ... The centripetal force is real, ... door of a car pushing sideways on you as the car turns a corner. ... One person might say there is a force backwards on the voodoo head. ... The only agent available for such a force is the string. ...
    (sci.physics.relativity)