How To Use Maps

This sample program shows how to use the GroupLab.Networking.Map object to keep a collection of attributes underneath a single key.

Note: although GroupLab.Networking.Map exists in current versions of the Shared Dictionary, the behaviour described here is not quite correct. This page has been tagged as requiring correction for a later time.

Download: SDMapDemo.zip

Maps are a great way of managing collections of data. Instead of using custom data objects, you can simply place the properties of the object into a Map.

Maps function in the same manner as a Hashtable or Hashmap - a key object is used to index a collection of values. In this case only string keys are allowed, however the value may be any serializable object.

When you wish to change the value of an attribute of the map, you do not need to get and set the entire map object - attributes in a map can be accessed by tagging the attribute's name on the end, separated by a ".". ex. "/path/key.attribute"

To be notified of key changes you can either subscribe to the map, or if you wish you can even subscribe to a particular attribute. When subscribed to the whole Map, the notification reason is of type SubscriptionNotification.AttributeChanged. In this case, the event object has an AttributeName property which gives you the key string for the property that was updated.

The following code snippet shows how to do some common things with a Map. Also see the download above for more details.

// Create and open the shared dictionary
SharedDictionary sd = new SharedDictionary();
sd.Open( true );

// Create the map locally
Map m = new Map()
m.Add( "prop1", "value1" );
m.Add( "prop2", 10 );

// Place the map into the Shared Dictionary
sd.Add( "/stuff/map", m );

// Change the value
sd[ "/stuff/map.prop1" ] = "Hello there!";
sd[ "/stuff/map.prop2" ] = 25;