Reputation: 65
Dim i = 2
Do While True
i += 1
If IsDBNull(TmDataSet.T.Rows(0)(i)) = True Then Exit Do
Dim new_t As New train
new_t.id = TmDataSet.T.Rows(0)(i)
Dim j = 0
Do Until IsDBNull(TmDataSet.T.Rows(j + 1)(i))
j += 1
Do Until (TmDataSet.T.Rows(j)(i) <> -1)
j += 1
Loop
If IsDBNull(TmDataSet.T.Rows(j + 1)(i)) Then Exit Do
Dim new_st As New station
new_st.t = TmDataSet.T.Rows(j)(i)
new_st.name = TmDataSet.T.Rows(j)(1)
new_st.id = TmDataSet.T.Rows(j)(2)
new_st.id_t = new_st.id.ToString & new_st.t
Dim new_st2 As New station
Do Until (TmDataSet.T.Rows(j + 1)(i) <> -1)
j += 1
Loop
new_st2.t = TmDataSet.T.Rows(j + 1)(i)
new_st2.name = TmDataSet.T.Rows(j + 1)(1)
new_st2.id = TmDataSet.T.Rows(j + 1)(2)
new_st2.id_t = new_st2.id.ToString & new_st2.t
Dim list As New List(Of station)
list.Add(new_st)
list.Add(new_st2)
new_t.st.Add(list)
Loop
per_network.Add(new_t)
Loop
' network = deep copy of per_network
vb >>> I just want to copy the content of per_network to network, I have tried ToList method but it was shallow copy and fail to perform clone method I didn't get it at all
Upvotes: 2
Views: 2210
Reputation: 33080
This is the code I used
Dim deepCopies = _dictOfBalances1.Keys.ToList
Basically deepCopies is a deep copy of _dictOfBalances1.Keys
This code won't work if deepCopies are shallow copies.
For Each code In deepCopies
If allthecoincode.Contains(code) Then
Else
_dictOfBalances1.Remove(code)
'deletedKeys.Add(code)
End If
Next
Upvotes: 0
Reputation: 18310
You can create an extension method where you serialize the object only to deserialize it again. This will create a new object with it's own references, thus a Deep Copy.
Public Module Extensions
<System.Runtime.CompilerServices.Extension()> _
Public Function DeepCopy(Of T)(ByVal Obj As T) As T
If Obj.GetType().IsSerializable = False Then Return Nothing
Using MStream As New MemoryStream
Dim Formatter As New BinaryFormatter
Formatter.Serialize(MStream, Obj)
MStream.Position = 0
Return DirectCast(Formatter.Deserialize(MStream), T)
End Using
End Function
End Module
Now you can just call:
Dim network As List(Of train) = per_network.DeepCopy()
EDIT:
These are the required imports for my code above:
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Upvotes: 4