12

Here's my code thats throwing an error saying Cannot convert type "int" to Cards.Suits and Cannot convert type "int" to Cards.Rank

private Card[] cards; public Deck() { cards = new Card[52]; for (int suitVal = 0; suitVal < 4; suitVal++) { for (int rankVal = 0; rankVal < 14; rankVal++) { cards[suitVal * 13 + rankVal - 1] = new Card((Suits)suitVal, (Rank)rankVal); } } } 

the cards constructor is

public readonly Suits suit; public readonly Rank rank; public Card(Suits newSuit, Rank newRank) { suit = newSuit; rank = newRank; } 

Now the Suits enum and Rank enum are as a regular deck of cards starting at ACE = 1 so on and suits are DIAMONDS, CLUBS, HEARTS, SPADES. Can anyone tell me why im getting the above error. The following code was taking from a book. Thanks!

*EDIT

 public enum ranks { ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, } public enum Suit { DIAMOND, CLUB, HEART, SPADE, } 
9
  • Can you show the definition for Suits and Rank? Commented Aug 2, 2013 at 5:31
  • Not a duplicate as that method is used and still returns an error Commented Aug 2, 2013 at 5:38
  • 1
    You're using (Suits)suitVal but the enum you posted is actually named Suit. Commented Aug 2, 2013 at 5:41
  • 5
    Your code defines enums ranks and Suit, but attempts to cast to types Rank and Suits. Make sure all of your type names match up. Commented Aug 2, 2013 at 5:42
  • Nope that does not work Error 1 The type or namespace name 'Suit' could not be found (are you missing a using directive or an assembly reference?) Commented Aug 2, 2013 at 5:44

4 Answers 4

2

According to your enum declarations, Suit is in the [0..3] range, while ranks is in the [1..13] range (pay attention, that ranks is not zero based), so the inner for loop should be corrected:

 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] ... 
Sign up to request clarification or add additional context in comments.

2 Comments

-1 because the above code will throw an error when both suitval and ranval are 0. which will evaluate this cards[suitVal * 13 + rankVal - 1] to cards[- 1]. Hence index out of range.
@Ehsan Ullah: Thank you, I've corrected index within cards[…]
1

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; } 

4 Comments

Intellisense is still directing me to using public readonly Suits suit; as oppsed to your answer. I should mention this is a class library.
i have updated Suits suit to Suit suit; You are passing enumeration as parameter which is Suit (in the code you gave above). not Suits.
This does not work when i had to do it it was Suits.Suit and Ranks.rank
@user2434321 please post your entire code. What is Suits? is it a class? what is Ranks? we cannot keep guessing
0

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.

Comments

0

This is running well but correct this

 for (int suitVal = 0; suitVal < 4; suitVal++) { for (int rankVal = 0; rankVal < 14; rankVal++) { cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal, (Rank)rankVal); } } 

Here

in cards[suitVal * 13 + rankVal - 1]

[Loop 1:] suitVal =0 rankVal=0 

[suitVal * 13 + rankVal - 1]= [0*13+0-1]= [-1] ! oops!

and in your prgm:

public readonly Suit suit; //not Suits public readonly ranks rank; //not Rank 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.