How to convert int to enum value?
This question already has an answer here:
根据你的枚举声明,Suit在[0..3]范围内,而等级在[1..13]范围内(注意,该等级不是以0为基础的),所以应该纠正内循环:
for (int rankVal = 0; rankVal < 13; rankVal++) // <- 14 changed for 13: [0..13] has the same length as [1..14]
{
cards[suitVal * 13 + rankVal] = new Card((Suits)suitVal, (Rank)(rankVal + 1)); // <- removed -1 from index; add 1 to rankVal, we need [1..14], not [0..13]
...
change your line in for like this
cards[suitVal * 13 + rankVal] = new Card(((Suit)suitVal), ((ranks)rankVal));
and as your class is taking enums in constructor so change it like this
public readonly Suit suit;
public readonly ranks rank;
public Card(Suit newSuit, ranks newRank)
{
suit = newSuit;
rank = newRank;
}
I will see it will get other error "Index was outside the bounds of the array"
When suitVal = 0 and rankVal = 0, cards[suitVal * 13 + rankVal - 1] =-1 which is outside array index.
链接地址: http://www.djcxy.com/p/22516.html上一篇: 如何将枚举与数据库ID进行映射
下一篇: 如何将int转换为枚举值?