System.Collections.Generic.Dictionary

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 datatype 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(key,value);
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(key,value)
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(key,value)
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(key,value)
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)
   snamesarray.GetValue(icount)
Next icount

You can also obtain a collection from the Dictionary
KeyCollection
ValueCollection


Does an Element Exist


If dicAuthors.Keys.Contains("002" 

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

End If

Removing an Element

dicAuthors.Remove(key) 
dicAuthors.Remove("001")


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