Saturday, April 18, 2009

How to use dictionary in [VB / C#]

This is how to declare, add / insert and read / view dictionary in C# and VB .net. But you have include library for reference
VB
'In header
Imports System.Collections.Generic

'In your codes section
Dim items As Dictionary(Of String, String) = New Dictionary(Of String, String)()

items.Add("The Key", "The Value")

For Each item As KeyValuePair(Of String, String) In items
Label1.Text = Label1.Text & "Key: " & item.Key.ToString() & ", Display: " & item.Value.ToString() & "
"

Next


C#
// In header
Using System.Collections.Generic;

// In your codes section
Dictionary<string, string> items = new Dictionary<string, string>();


items.Add("The Key", "The Value");

foreach (KeyValuePair<string, string> item in items)
{
Label1.Text = Label1.Text + "Key: " + item.Key.ToString() + ", Display: " + item.Value.ToString() + "<br>";
}


I prefer using dictionary rather than array. Dictionary is suitable to be used when we know its data type

No comments:

Post a Comment