A
ags5406
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
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