I have a class called ReDictionary
Edit: My class declaration
Public Class ReDictionary(Of TKey, TValue)
Inherits Dictionary(Of TKey, TValue)
which provides minor enhancements for my purposes of a dictionary with things like this.
' Wraps TryGetValue
Public Function ValueOr(key, OnNoValue)
dim TossBool = Me.TryGetValue(key, TossObj)
If TossBool Then
Return TossObj
Else
Return OnNoValue
End If
End Function
I'm trying to write a .Clone()
method (shallow) as part of the class.
I've tried a function that uses a TopLevelDictionary.clone(src,dest)
syntax and a sub that uses an TopLevelDict("Entry").Clone(src)
syntax
Public Sub Clone(srcKey As TKey, destKey As TValue) As ReDictionary(Of object, object)
Dim newbook As New ReDictionary(Of Object, Object)
For Each kvp As KeyValuePair(Of Object, Object) In Me(srcKey)
newbook(kvp.Key) = kvp.Value.ToString()
Next
Me(destKey) = newbook
End Sub
This is almost a quasi-code. I've tried probably every combination of TKey/TValue
, Object/Object
, even down the the specific of String/String
which the two dictionaries I want to clone happen to be. I've also tried not specifying argument/return type
The errors I typically get are Cannot Convert ReDictionary(...) to ReDictionary(...)
such as when the when the functions temporary ReDictionary is (Object, Object)
and the source ReDictionary is (String, String)
.
I'd like to avoid making my public dictionary (Object, Object)
because it seems wasteful on memory.
My two dictionaries are populated within the same function on form load. One is a public and the other is local to the function but passes values (a description selected by language) global dictionary.
I've realized I could probably easily write a clone function (not part of ReDictionary
that used a syntax like Dict(NewEntry) = CloneEntry(Dict("Entry")
. I had something like this before when I was just trying to clone object in the Public Dictionary.
Private Function CloneObj(key) As ReDictionary(Of String, String)
Dim inObj = New ReDictionary(Of String, String)
For Each kvp As KeyValuePair(Of String, String) In pLib(key)
inObj(kvp.Key) = pLib(key)(kvp.Key)
Next
Return inObj
End Function
Although I might possibly run conflicts when I try to relax the object type.
- The Global dictionary is a
Dictionary(String) of Dictionaries(String) of Strings
, - The local dictionary is a
Dictionary(String) of cTranslations(class) with String properties
.
At that point, two functions could do it, but I'm just more determined, if it's possible.
Update: This is the version I'm currently trying to use
Public Class ReDictionary(Of TKey, TValue)
Inherits Dictionary(Of TKey, TValue)
Public Function Clone(srcKey As Object) As ReDictionary(Of TKey, TValue)
Dim inObj As New ReDictionary(Of TKey, TValue)
For Each kvp As KeyValuePair(Of TKey, TValue) In Me
inObj(kvp.Key) = kvp.Value
Next
Return inObj
End Function
End Class
This is an example usage of that syntax.
Dim Films As New ReDictionary(Of String, ReDictionary(Of String, String)
Films("Edge of Tomorrow") = New ReDictionary(Of String, String)
Films("Edge of Tomorrow")("Description") = "Tom Crues goes to war on Groundhog's Day"
Films("Live, Die, Repeat") = Films.Clone("Edge of Tomorrow")
Films("Birdman") = New ReDictionary(Of String, String)
Films("Birdman")("Description") = "Michael Keyton, who played a superhero 20 years ago, plays an actor who also did"
Films("The Unexpected Virtue of Ignorance") = Films.Clone("Birdman")
I actually change some properties of some of the cloned items, which is why I need to clone rather than just one entry being a reference to a sibling entry.
The function itself is actually not causing any errors, but when I try to use it I get (copied straight from debugger, only indented so that it's easy to read):
The error is pretty obvious
Value of type 'ReDictionary(Of String, ReDictionary(Of String, String))'
cannot be converted to 'ReDictionary(Of String, String)
If I change In Me
to In Me(srckey)
, I get
Expression of type TValue which is not a collection type
.
Changing the typing of srcKey to TValue
or TKey
also results in errors, as does removing the typing of srckey.