从另一个创建字典实例 [英] Creating dictionary instance from an other
问题描述
我有以下vb代码。我创建了一个名为ArabicCodesLettersShapesDic的字典作为公共字典。我试图将这个字典的元素存储到另一个名为ArabicCodesLettersShapesDicTemp的本地字典中,当我通过添加或删除项目来更新本地字典时,这些更新出现在原始字典中,并且我不希望更新原始(公共)。所以我想在不更新原始版本的情况下更新本地版本。我该怎么做这个机制。
功能 FindingMissingArabicLetter( ByVal AllArabicCodes As String ) As String
Dim MissingArabicLetters As String =
Dim MyArray()作为 String = AllArabicCodes.Split( New String (){Environment.NewLine} ,StringSplitOptions.None)
Dim ArabicCodesLettersShapesDicTemp As ñ ew 字典( 字符串,字符串)
ArabicCodesLettersShapesDicTemp = ArabicCodesLettersShapesDic
System.Array.Sort( Of String )(MyArray)
对于 每个 arrItem 正如 字符串 在 MyArray
If ArabicCodesLettersShapesDicTemp.ContainsKey(arrItem)= True 然后
ArabicCodesLettersShapesDicTemp.Remove(arrItem)
结束 如果
下一步
对于 每个 dicItem 作为 字符串 在 ArabicCodesLettersShapesDicTemp.Keys
MissingArabicLetters = MissingArabicLetters& (ArabicCodesLettersShapesDicTemp.Item(dicItem))& vbNewLine
下一步
' For Each Item As KeyValuePair(Of String,String)in ArabicCodesLettersShapesDic
' MessageBox.Show(item.Key ++ item.Value)
' 下一步
返回 MissingArabicLetters
结束 功能
我尝试了什么:
我试图搜索这个,但我找不到sol。
你的临时字典只是对原文的引用,所以你只有一个实际的字典。您需要通过复制或克隆原始文件来创建临时文件。请参阅 Object.MemberwiseClone Method(System) [ ^ ]。
问题出在这里;
ArabicCodesLettersShapesDicTemp = ArabicCodesLettersShapesDic
要实现您想要的功能,请使用Add()
过程或其他
Dictionary
的复制构造函数类型:
Dim ArabicCodesLettersShapesDicTemp As 新字典( 字符串,字符串)(ArabicCodesLettersShapesDic)
资料来源:
词典< TKEY的,TValue>构造函数(IDictionary< TKey,TValue>) [ ^ ]
I have the following vb code. I created a dictionary called ArabicCodesLettersShapesDic as public. I tried to store the element of this dictionary to another local dictionary called ArabicCodesLettersShapesDicTemp and when I update the local dictionary by adding or removing items these update appear in the original one and I don't want the original (public) to be updated. So I want to update the local one without updating the original one. How can I do this mechanism.
Function FindingMissingArabicLetter(ByVal AllArabicCodes As String) As String
Dim MissingArabicLetters As String = ""
Dim MyArray() As String = AllArabicCodes.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
Dim ArabicCodesLettersShapesDicTemp As New Dictionary(Of String, String)
ArabicCodesLettersShapesDicTemp = ArabicCodesLettersShapesDic
System.Array.Sort(Of String)(MyArray)
For Each arrItem As String In MyArray
If ArabicCodesLettersShapesDicTemp.ContainsKey(arrItem) = True Then
ArabicCodesLettersShapesDicTemp.Remove(arrItem)
End If
Next
For Each dicItem As String In ArabicCodesLettersShapesDicTemp.Keys
MissingArabicLetters = MissingArabicLetters & (ArabicCodesLettersShapesDicTemp.Item(dicItem)) & vbNewLine
Next
'For Each item As KeyValuePair(Of String, String) In ArabicCodesLettersShapesDic
' MessageBox.Show(item.Key + " " + item.Value)
'Next
Return MissingArabicLetters
End Function
What I have tried:
I tried to search about this but I couldn't find sol.
Your temp dictionary is just a reference to the original, so you only have one actual dictionary. Your temp one needs to be created by copying or cloning the original. See Object.MemberwiseClone Method (System)[^].
The issue lies here;
ArabicCodesLettersShapesDicTemp = ArabicCodesLettersShapesDic
To achieve what you want is to manually populate items in your temporary Dictionary from the original one, by using theAdd()
procedure or other
There is a copy constructor forDictionary
type:
Dim ArabicCodesLettersShapesDicTemp As New Dictionary(Of String, String)(ArabicCodesLettersShapesDic)
Source:
Dictionary<TKey, TValue> Constructor (IDictionary<TKey, TValue>)[^]
这篇关于从另一个创建字典实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!