System.Collections.Generic.Dictionary

Added in .NET 2.0
This type of collection represents a collection of 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


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

myDictionary.Add("Mon", new System.DateTime(2020,10,3));
myDictionary.Add("Tue", new System.DateTime(2021,10,3));
myDictionary.Add("Wed", new System.DateTime(2022,10,3));

if (myDictionary.ContainsKey("Tue")
{
   System.DateTime myDateTime;
   myDateTime = (System.DateTime)myDictionary["Tue"];
}

This is how you can access the keys and values
The entry in the dictionary is an instance of KeyValuePair<string, DateTime>.

foreach (System.Collections.Generic.KeyValuePair<string, System.DateTime> kvp in myDictionary) 
{
   string myText;
   myText = System.String.Format("Key={0}, Value={1}",kvp.Key, kvp.Value);
}

Print the Keys

System.Collections.Generic.Dictionary<string, System.DateTime>.KeyCollection myKeys; 
myKeys = myDictionary.Keys;
foreach (string myKey in myKeys)
{
   System.Console.WriteLine(myKey);
}

Print the Values

System.Collections.Generic.Dictionary<string, System.DateTime>.ValueCollection myValues; 
myValues = myDictionary.Values;
foreach (System.DateTime myValue in myValues)
{
   System.Console.WriteLine(myValue);
}

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 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>();

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


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