alpeshjain
New Member
- Joined
- Oct 5, 2017
- Messages
- 7
Hi,
My question might sound a bit illogical and even I never thought about something like this before I encountered a problem myself.
Let me try to explain what I am looking to do. Here is the code I have, it is only one function and there are a few others in the module but I think this should be enough to explain the problem I have.
So as you can see, I am trying to create kind of a map and for that I am using dictionary and as item of dictionary I have another dictionary.
At the first underlined line you can see that I am making a recursive call to same method until one of first 2 if conditions are true.
Once one of them is true, it returns a dictionary object to caller (second underlined line).
The returned dictionary object is then either first modified and then added as a value to associated with another key or directly added as a value associated with another key.
Here comes the problem, if the dictionary object is first modified then the changes are also reflected in the original dictionary which is already stored as a values associated with another key.
So my conclusion is, the issue is happening because the function return the reference of Dictionary object and not a copy/value. So when the object is modified, it also modifies the original Object.
Now I want to understand if there is way to return a copy/value of object rather than reference.
PS: I have tried my best to explain the problem, but I know it can be a bit tricky, so if you have any confusion please let me know and I can clear them.
Thanks
My question might sound a bit illogical and even I never thought about something like this before I encountered a problem myself.
Let me try to explain what I am looking to do. Here is the code I have, it is only one function and there are a few others in the module but I think this should be enough to explain the problem I have.
Code:
Function ConnectToNonZeroSuccessor(sourceSh As Worksheet, taskId As String, sucMap As Dictionary) As DictionaryDim sucRows() As Integer
Dim sucTask As String
Dim dur As Integer
Dim listOfSuc As Dictionary
If taskId = 378 Then
DoEvents
End If
sucRows = searchValueMultipleRow(sourceSh, taskId, "K:K")
If sucRows(0) <> -1 Then
For Each sucRow In sucRows
sucTask = Trim(sourceSh.Cells(sucRow, 1))
dur = sourceSh.Cells(sucRow, 4)
If dur <> 0 Then
If Not sucMap.Exists(taskId) Then
Set listOfSuc = New Dictionary
'listOfSuc.item(sucTask) = "1"
Set sucMap.item(taskId) = listOfSuc
End If
sucMap.item(taskId).item(sucTask) = "1"
ElseIf sucMap.Exists(sucTask) Then
If Not sucMap.Exists(taskId) Then
Set listOfSuc = New Dictionary
Set sucMap.item(taskId) = listOfSuc
End If
For Each key In sucMap.item(sucTask).Keys
sucMap.item(taskId).item(key) = "1"
Next key
Else
[U] Set listOfSuc = ConnectToNonZeroSuccessor(sourceSh, sucTask, sucMap)[/U]
If Not sucMap.Exists(taskId) Then
Set sucMap.item(taskId) = listOfSuc
Else
For Each key In listOfSuc.Keys
sucMap.item(taskId).item(key) = "1"
Next key
End If
End If
Next sucRow
If sucMap.item(taskId) Is Nothing Then
MsgBox sucTask & " Does not have a non zero successor"
Exit Function
End If
[U] Set ConnectToNonZeroSuccessor = sucMap.item(taskId)[/U]
End If
End Function
So as you can see, I am trying to create kind of a map and for that I am using dictionary and as item of dictionary I have another dictionary.
At the first underlined line you can see that I am making a recursive call to same method until one of first 2 if conditions are true.
Once one of them is true, it returns a dictionary object to caller (second underlined line).
The returned dictionary object is then either first modified and then added as a value to associated with another key or directly added as a value associated with another key.
Here comes the problem, if the dictionary object is first modified then the changes are also reflected in the original dictionary which is already stored as a values associated with another key.
So my conclusion is, the issue is happening because the function return the reference of Dictionary object and not a copy/value. So when the object is modified, it also modifies the original Object.
Now I want to understand if there is way to return a copy/value of object rather than reference.
PS: I have tried my best to explain the problem, but I know it can be a bit tricky, so if you have any confusion please let me know and I can clear them.
Thanks