dodotu
dodotu

Reputation: 115

Cannot implicitly convert type Dictionary<String,int> to int

I am trying to develop an Xna project and make a collision detection mechanism.I have a Dictionary object like :

Dictionary<string, int> boneIndices = new Dictionary<string, int>();

and I want to get indixes from this dictionary like at the above line :

 int boneIndex = this.animator.skinningData.BoneIndices;

but I take an error which is at the topic.

How can ı solve this error?

Upvotes: 1

Views: 2462

Answers (2)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

If you're not sure the key exists within the dictionary, you can take a safer approach using TryGetValue:

string key = "key";
int indice = 0;

if (!animator.skinningData.BoneIndices.TryGetValue(key, out indice)
{
     // If you get here, the key doesn't exist
}

Using an indexer directly may throw a KeyNotFoundException if the key isn't present in the dictionary.

Upvotes: 1

Ondrej Janacek
Ondrej Janacek

Reputation: 12616

You forgot to provide a key to find a value, like following:

int boneIndex = this.animator.skinningData.BoneIndices["someKey"];

What the error is saying is that you are assigning the whole dictionary of type Dictionary<string, int> to a variable of type int. Apart from using indexer as I showed you, there are some other ways to get a value from a dictionary. You can find them on MSDN.

Upvotes: 7

Related Questions