Search code examples
c#castingoperator-precedenceidentifierdot-operator

Casting giving 'identifier expected' error even though I am respecting dot operator precedence (C#)


Originally my code was:

spreadRight = equipmentSlots[5].(EquippableItem)Item.DamageVariance;

However this threw errors and I learned about the dot operator having higher precedence than casting. So I redid it as:

spreadRight = equipmentSlots[5].((EquippableItem)Item).DamageVariance;

However, it is still giving me 'identifier expected' error. Is it still a problem with operator precedence, or am I doing something else wrong?

Thanks.


Solution

  • If you want to cast Item property of element from equipmentSlots collection to EquippableItem, you should change your code like this:

    spreadRight = ((EquippableItem)equipmentSlots[5].Item).DamageVariance;