1

My code looks like this:

var score = ((Dictionary<string, object>)entry.Value)["score"];
print("score: " + score + "!");
FBManager.instance.friends[id].score = (int) score;


//OUTPUT: 
//score: 5!
//InvalidCastException: Specified cast is not valid.

Why can't I cast score to int?

I'm pretty sure entry.Value is a Dictionary<string, object> because I printed entry.Value.GetType() and it said exactly this.

Daniel
  • 7,357
  • 7
  • 32
  • 84

1 Answers1

2

See what you get when you do: Console.WriteLine(score.GetType())

If it is System.String you will need to convert (Convert.ToInt32(score)) or int.Parse(score). If it is System.Int32 you should be fine to cast. If it is anything else (double, decimal, etc.) just do the Convert thing :)

Good Luck!

Mirko
  • 4,284
  • 1
  • 22
  • 19