This type of collection represents a collection on keys and values
Every value has a unique key associated with it.
Objects made available through unique keys.
You can indicate the data type of both the key and value at declaration
Items are stored through the related KeyValuePair class

DictionaryEntry is actually a KeyValuePair
This is built-in when you have a dictionary

Different types of key,pairs

String Items - System.Collections.Generic.Dictionary(Of String, String) - These are referenced in code as Dictionary
String Array Items - System.Collections.Generic.Dictionary(Of String, String()) - These are referenced in code as Dictionary Arrays

Group of Strings

System.Collections.Generic.Dictionary<string, string> dicAuthors; 
dicAuthors = new System.Collections.Generic.Dictionary<string, string>();

dicAuthors.Add(key, value);
dicAuthors.Add("001", "my first string");
dicAuthors.Add("002", "my second string");
Dim dicAuthors As System.Collections.Generic.Dictionary(Of String, String) 
dicAuthors = New System.Collections.Generic.Dictionary(Of String, String)

dicAuthors.Add(key, value)
dicAuthors.Add("001", "my first string")
dicAuthors.Add("002", "my second string")

Group of String Arrays

System.Collections.Generic.Dictionary<string, string[]> adicAuthors; 
adicStrings = new System.Collections.Generic.Dictionary<string, string[]>();

adicStrings.Add("Item1", ??);
Dim adicStrings As New System.Collections.Generic.Dictionary(Of String, String()) 
adicStrings = New System.Collections.Generic.Dictionary(Of String, String())

adicStrings.Add("Item1", ??)

Group of DataGridViewRows

System.Collections.Gernic.Dictionary<int, System.Windows.Forms.DataGridViewRow> dicRows; 

dicRows = new System.Collections.Generic.Dictionary<int, System.Windows.Forms.DataGridView>();
dicRows.Add(objdatagridview.cells(0).rowindex, objdatagridviewrow)
Dim dicRows As System.Collections.Generic.Dictionary(Of Integer, System.Windows.Forms.DataGridViewRow) 

dicRows = New System.Collections.Generic.Dictionary(Of Integer, System.Windows.Forms.DataGridViewRow)
dicRows.Add(objdatagridview.cells(0).rowindex, objdatagridviewrow)

Group of Objects

System.Collections.Generic.Dictionary<string, MyObject>(); 
dicObjects = new System.Collections.Generic.Dictionary<string, MyObject>();
Dim dicObjects As System.Collections.Generic.Dictionary(Of String, MyObject) 
dicObjects = New System.Collections.Generic.Dictionary(Of String, MyObject)

Looping Through Elements

foreach (System.Collections.Generic.KeyValuePair<int, string> objKeyValuePair in dicItems) 
   Debug.Print objKeyValuePair.Key + " - " + objKeyValuePair.Value;
Dim objKeyValuePair As System.Collections.Generic.KeyValuePair(Of Integer, String) 

For Each objKeyValuePair In dicItems
   Debug.Print objKeyValuePair.Key & " - " & objKeyValuePair.Value
Next objKeyValuePair

For icount As Integer = 0 To adicAuthors.Count 
   skey = adicAuthors.ElementAt(icount).Key

Next icount

Dim snamesarray As System.String 

snamesarray = dicAuthors.ToArray
For icount As Integer = 0 To (dicAuthors.Count - 1)
Next icount

You can also obtain a collection from the Dictionary

Does an Element Exist

If dicAuthors.Keys.Contains("002" 

If dicAuthors.ContainsKeys("002") = True Then

End If

Removing an Element


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext