c# - How to compare element values between two Ordered Dictionaries -
i trying compare values in elements of 2 ordered dictionaries. unable find right coding @ elements @ index 0 , convert them dictionaryentrys. trying use following code:
dictionaryentry card1 = (dictionaryentry)(player1.hand[0]); dictionaryentry card2 = (dictionaryentry)(player2.hand[0]); if ((int)card1.value > (int)card2.value) { // stuff }
the code passes intellisense, blows invalid cast. can code
var card1 = player1.hand[0];
but won't allow me retrieve card1.value. far can tell, card1 needs converted dictionaryentry, , don't know how it.
any appreciated. thanks.
the indexer in ordereddictionary
implemented follows:
public object this[int index] { { return ((dictionaryentry)objectsarray[index]).value; } set { ... } }
it's returning value, not key/value pair. that's why can't cast dictionaryentry
.
do comparison instead, casting return value of indexer directly int
:
if ((int)player1.hand[0] > (int)player2.hand[0]) { // stuff }
from comment, sounds want able modify key too.
afaik, you'll have use key or index value, remove old element , add/insert new one:
var value = (int)player1.hand[0]; player1.hand.removeat(0); player1.hand.add("somenewkey", value); var value = (int)player1.hand["oldkey"]; player1.hand.remove("oldkey"); player1.hand.add("somenewkey", value);
Comments
Post a Comment