Silverlight Tip of the Day #70 – Data Collection Support in Silverlight
Silverlight provides a number of options for data collection, storage and retrieval. In this Tip I will be walking you through various ways of doing this. You will need to add a using statement in your page to reference System.Collections;
BitArray. The BitArray is a class that you can instantiate that manages a collection of bit values which are represented as either true (1 = bit is on) or false (0 =bit is off). The following example below is an implementation of BitArray. Note that there are many different constructor options for the BitArray, this is just one.
BitArray bitArray = new BitArray(4);
// Add
bitArray[0] = true;
bitArray[1] = false;
bitArray[2] = false;
bitArray[3] = true;
// Retreive
if (true == bitArray[1])
{
//...
}
List. The List collection allows you to store any type of data that can than be accessed by a index value. In the case below we use a string but you can replace string with any type of object you want.
List<string> test = new List<string>();
// Add data
test.Add("Hello");
test.Add("There");
test.Add("Goodbye");
// Retrieve "Goodbye"
string data = test[2];
Stack. A stack is always last in first out. That is, the data that was inserted on the stack via a call to Push() will be the first retrieved when the call to Pop() is made. Like the List collection above, you can store any type of object in it. For our example below we are using an integer.
Stack<int> stack = new Stack<int>();
stack.Push(5);
stack.Push(3);
stack.Push(4);
// Pop the 4 off the stack.
int test = stack.Pop();
Queue. A queue is first in first out. Objects are added via the Enqueue() call and retrieved and removed via the Dequeue() call. In the example below, we enqueue three strings. Then, a call to Dequeue() removes the first string insert which was “First”. If we were to make another call to Dequeue() we would retrieve the string “Second”.
// Add data
Queue<string> queue = new Queue<string>();
queue.Enqueue("First");
queue.Enqueue("Second");
queue.Enqueue("Third");
// Retrieve the string "First"
string test = queue.Dequeue();
LinkedList. A LinkedList is a linked list of nodes. You have direct access to the head and tail of the list and can enumerate through the list from beginning to end. . In the example below we declare a class called Node that we store in the linked list.
class Node
{
private int _ID;
public Node(int ID)
{
_ID = ID;
}
public int ID
{
get { return _ID; }
set { ID = value; }
}
};
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
// Add Nodes
LinkedList<Node> _list = new LinkedList<Node>();
LinkedListNode<Node> headNode = _list.AddFirst(new Node(432));
LinkedListNode<Node> lastNode = _list.AddAfter(headNode, new Node(200));
lastNode = _list.AddAfter(lastNode, new Node(451));
lastNode = _list.AddAfter(lastNode, new Node(14));
lastNode = _list.AddAfter(lastNode, new Node(55));
// Retrieve Nodes
foreach (Node node in _list)
{
int ID = node.ID;
}
}
}
Dictionary. Dictionaries store data via a key / value pair where the key is used to to identify and retrieve the item being stored and the value is the actual data being stored.
Dictionary<int, string> myData = new Dictionary<int, string>();
// Store Data
myData.Add(4324, "Cat");
myData.Add(331, "Dog");
myData.Add(442, "Lion");
myData.Add(3444, "Tiger");
// Retrieve "Lion" by passing the key 3444
string animal = myData[442];
Thank you,
--Mike Snow
Subscribe in a reader