This top code snippet works if the Dictionary is accessed directly and it’s not returned in a call back.
properties = new Dictionary<string, object>();
properties.Add("Blue", "Dan");
properties.Add("Red", "Joe");
foreach(KeyValuePair<string, object> pair in properties)
{
if(pair.Value == "Dan" )
{
// executed when pair.Value == "Dan"
Debug.Log(pair.Value + "'s color is " + pair.Key +"\n");
}
Debug.Log("KEY:" + pair.Key + " = VALUE:" + pair.Value + "\n");
}
OUTPUT:
Dan’s color is Blue
KEY:Blue = VALUE:“Dan”
KEY:Red = VALUE:“Joe”
The above Dictionatry is made of the same type as the properties argument returned to
the onUserChangeRoomProperty() callBack below. But the same code as above doesn’t work
in the callback. It’s as if the Dictionatry value <type: Object> can’t be resolved or
converted to an accurate string that works in a comparison.
public void onUserChangeRoomProperty(RoomData roomData, string sender, Dictionary<String, Object> properties)
{
foreach (KeyValuePair<String, Object> pair in properties)
{
// NOTE sender == "Dan" in this case
// if( sender == "Dan" ) // This works as expected.
// if( sender == pair.Value.ToString() ) // This doesn't work.
if( sender == pair.Value ) // This doesn't work.
{
// Never executed
Debug.Log(pair.Value + "'s color is " + pair.Key +"\n");
}
Debug.Log("KEY:" + pair.Key + " = VALUE:" + pair.Value + "\n");
}
}
OUTPUT:
KEY:Blue = VALUE:“Dan”
KEY:Red = VALUE:“Joe”