Last active
December 18, 2015 19:49
-
-
Save TokyoDan/5835982 to your computer and use it in GitHub Desktop.
Is a Dictionary format different when it is returned as a callback compared to when it is accessed directly?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment