ASKER
ASKER
ASKER
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim theEmployee1 As New EmployeeDeep("John", "Smith")
Dim theEmployee2 As New EmployeeDeep("Tom", "Jones")
Dim theEmployee3 As New EmployeeDeep("Joseph", "Rattz")
Dim theEmployee4 As New EmployeeDeep("Scott", "Duffy")
theEmployee1.Boss = theEmployee2
theEmployee2.Boss = theEmployee3
theEmployee3.Boss = theEmployee4
Dim theEmployee11 As New EmployeeDeep("Bill", "Smith")
Dim theEmployee21 As New EmployeeDeep("Sam", "Jones")
Dim theEmployee31 As New EmployeeDeep("Alice", "Rattz")
Dim theEmployee41 As New EmployeeDeep("Marry", "Duffy")
theEmployee11.Boss = theEmployee21
theEmployee21.Boss = theEmployee31
theEmployee31.Boss = theEmployee41
Dim emplyeeSortedList As New SortedList(Of String, EmployeeDeep)
emplyeeSortedList.Add("Employee1", theEmployee1)
emplyeeSortedList.Add("Employee11", theEmployee11)
emplyeeSortedList.Add("ClonedEmployee100", emplyeeSortedList("Employee1").Clone())
emplyeeSortedList.Add("ClonedEmployee101", emplyeeSortedList("Employee11").Clone())
MessageBox.Show("The object theEmployee1 is the same object ClonedEmployee : " _
& (emplyeeSortedList("Employee1") Is emplyeeSortedList("ClonedEmployee100")).ToString & Environment.NewLine _
& "The object theEmployee.Boss is the same object EmplyeeClone.Boss : " _
& (emplyeeSortedList("Employee1").Boss Is emplyeeSortedList("ClonedEmployee100").Boss).ToString & Environment.NewLine _
& "The object theEmployee.Boss.Boss is the same object EmplyeeClone.Boss.Boss : " _
& (emplyeeSortedList("Employee1").Boss.Boss Is emplyeeSortedList("ClonedEmployee100").Boss.Boss).ToString & Environment.NewLine _
& "The object theEmployee.Boss.Boss.Boss is the same object EmplyeeClone.Boss.Boss.Boss : " _
& (emplyeeSortedList("Employee1").Boss.Boss.Boss Is emplyeeSortedList("ClonedEmployee100").Boss.Boss.Boss).ToString)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim theEmployee1 As New Employee("John", "Smith")
Dim theEmployee2 As New Employee("Tom", "Jones")
Dim theEmployee3 As New Employee("Joseph", "Rattz")
Dim theEmployee4 As New Employee("Scott", "Duffy")
theEmployee1.Boss = theEmployee2
theEmployee2.Boss = theEmployee3
theEmployee3.Boss = theEmployee4
Dim ClonedEmployee As Employee = theEmployee1.Clone()
MessageBox.Show("The object theEmployee1 is the same object ClonedEmployee : " _
& (theEmployee1 Is ClonedEmployee).ToString & Environment.NewLine _
& "The object theEmployee.Boss is the same object EmplyeeClone.Boss : " _
& (theEmployee1.Boss Is ClonedEmployee.Boss).ToString & Environment.NewLine _
& "The object theEmployee.Boss.Boss is the same object EmplyeeClone.Boss.Boss : " _
& (theEmployee1.Boss.Boss Is ClonedEmployee.Boss.Boss).ToString & Environment.NewLine _
& "The object theEmployee.Boss.Boss.Boss is the same object EmplyeeClone.Boss.Boss.Boss : " _
& (theEmployee1.Boss.Boss.Boss Is ClonedEmployee.Boss.Boss.Boss).ToString)
End Sub
ASKER
Dim emplyeeSortedList As New SortedList(Of String, EmployeeDeep)
Dim emplyeeSortedListCopy As New SortedList(Of String, EmployeeDeep)
' Create, add and process EmployeeDeep
' .....
' Make a deep copy of all EmployeeDeep
For Each empKey As String In emplyeeSortedList.Keys
emplyeeSortedListCopy.Add(empKey, emplyeeSortedList(empKey).Clone())
Next
ASKER
Imports System
Imports System.Collections.Generic
Module Module1
Public Class EmployeeDeep
Implements ICloneable
Public m_FirstName As String
Public m_LastName As String
Public m_Boss As EmployeeDeep
Public Sub New()
End Sub
Public Sub New(ByVal first As String, ByVal last As String)
m_FirstName = first
m_LastName = last
End Sub
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim theClone As New EmployeeDeep
theClone.m_FirstName = Me.m_FirstName
theClone.m_LastName = Me.m_LastName
' THIS IS NOT CORRECT, WILL GENERATE DIFFERENT OBJECTS FOR THE SAME BOSS OF 2 EMPLOYEES
If m_Boss IsNot Nothing Then theClone.m_Boss = Me.m_Boss.Clone()
' THIS IS NOT CORRECT, WILL GENERATE A REFERENCE TO THE OLD OBJECT
' theClone.m_Boss = Me.m_Boss
Return theClone
End Function
End Class
Sub Main()
Dim theEmployee0 As New EmployeeDeep("Jaime", "Olivares")
Dim theEmployee1 As New EmployeeDeep("John", "Smith")
Dim theEmployee2 As New EmployeeDeep("Tom", "Jones")
Dim theEmployee3 As New EmployeeDeep("Joseph", "Rattz")
Dim theEmployee4 As New EmployeeDeep("Scott", "Duffy")
' BOTH EMPLOYEE 0 AND 1 MUST SHARE THE SAME BOSS
theEmployee0.m_Boss = theEmployee2
theEmployee1.m_Boss = theEmployee2
theEmployee2.m_Boss = theEmployee3
theEmployee3.m_Boss = theEmployee4
' PUT ALL THEM IN A SORTED LIST
Dim emplyeeSortedListOriginal As New SortedList(Of String, EmployeeDeep)
emplyeeSortedListOriginal.Add("E0", theEmployee0)
emplyeeSortedListOriginal.Add("E1", theEmployee1)
emplyeeSortedListOriginal.Add("E2", theEmployee2)
emplyeeSortedListOriginal.Add("E3", theEmployee3)
emplyeeSortedListOriginal.Add("E4", theEmployee4)
' CLONE THE SORTED LIST
Dim emplyeeSortedListCopy As New SortedList(Of String, EmployeeDeep)
For Each kvp As KeyValuePair(Of String, EmployeeDeep) In emplyeeSortedListOriginal
emplyeeSortedListCopy.Add(kvp.Key, kvp.Value.Clone)
Next
' CHANGE THE E0's BOSS NAME
emplyeeSortedListCopy("E0").m_Boss.m_FirstName = "TEST"
' BOTH E0's AND E1's BOSS SHOULD HAVE THE SAME NAME
Console.WriteLine("E0's Boss name: {0}", emplyeeSortedListCopy("E0").m_Boss.m_FirstName)
Console.WriteLine("E1's Boss name: {0}", emplyeeSortedListCopy("E1").m_Boss.m_FirstName)
Console.ReadKey()
End Sub
End Module
ASKER
Imports System
Imports System.Collections.Generic
Module Module1
Public Class EmployeeDeep
Implements ICloneable
Public m_FirstName As String
Public m_LastName As String
Public m_Boss As EmployeeDeep
Public Sub New()
End Sub
Public Sub New(ByVal first As String, ByVal last As String)
m_FirstName = first
m_LastName = last
End Sub
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim theClone As New EmployeeDeep
theClone.m_FirstName = Me.m_FirstName
theClone.m_LastName = Me.m_LastName
' DO NOT CLONE THE BOSS <----------- MODIFIED
' THIS IS NOT CORRECT, WILL GENERATE DIFFERENT OBJECTS FOR THE SAME BOSS OF 2 EMPLOYEES
'If m_Boss IsNot Nothing Then theClone.m_Boss = Me.m_Boss.Clone()
' THIS IS NOT CORRECT, WILL GENERATE A REFERENCE TO THE OLD OBJECT
theClone.m_Boss = Me.m_Boss
Return theClone
End Function
End Class
Sub Main()
Dim theEmployee0 As New EmployeeDeep("Jaime", "Olivares")
Dim theEmployee1 As New EmployeeDeep("John", "Smith")
Dim theEmployee2 As New EmployeeDeep("Tom", "Jones")
Dim theEmployee3 As New EmployeeDeep("Joseph", "Rattz")
Dim theEmployee4 As New EmployeeDeep("Scott", "Duffy")
' BOTH EMPLOYEE 0 AND 1 MUST SHARE THE SAME BOSS
theEmployee0.m_Boss = theEmployee2
theEmployee1.m_Boss = theEmployee2
theEmployee2.m_Boss = theEmployee3
theEmployee3.m_Boss = theEmployee4
' PUT ALL THEM IN A SORTED LIST
Dim emplyeeSortedListOriginal As New SortedList(Of String, EmployeeDeep)
emplyeeSortedListOriginal.Add("E0", theEmployee0)
emplyeeSortedListOriginal.Add("E1", theEmployee1)
emplyeeSortedListOriginal.Add("E2", theEmployee2)
emplyeeSortedListOriginal.Add("E3", theEmployee3)
emplyeeSortedListOriginal.Add("E4", theEmployee4)
' CLONE THE SORTED LIST
Dim emplyeeSortedListCopy As New SortedList(Of String, EmployeeDeep)
For Each kvp As KeyValuePair(Of String, EmployeeDeep) In emplyeeSortedListOriginal
emplyeeSortedListCopy.Add(kvp.Key, DirectCast(kvp.Value.Clone, EmployeeDeep))
Next
' RE-SCAN TO SET BOSS PROPERLY <---------------- ADDED THIS BLOCK
Dim index As Integer
For Each kvp As KeyValuePair(Of String, EmployeeDeep) In emplyeeSortedListCopy
index = emplyeeSortedListOriginal.IndexOfValue(emplyeeSortedListCopy(kvp.Key).m_Boss)
If index <> -1 Then
kvp.Value.m_Boss = emplyeeSortedListCopy.Values(index)
End If
Next
' CHANGE THE E0's BOSS NAME
emplyeeSortedListCopy("E0").m_Boss.m_FirstName = "TEST"
' BOTH E0's AND E1's BOSS SHOULD HAVE THE SAME NAME
Console.WriteLine("E0's Boss name: {0}", emplyeeSortedListCopy("E0").m_Boss.m_FirstName)
Console.WriteLine("E1's Boss name: {0}", emplyeeSortedListCopy("E1").m_Boss.m_FirstName)
Console.ReadKey()
End Sub
End Module
ASKER
ASKER
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.
TRUSTED BY