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,
    }
MikaAK
  • 2,334
  • 6
  • 26
  • 53
  • Can you show the definition for `Suits` and `Rank`? – p.s.w.g Aug 02 '13 at 05:31
  • Not a duplicate as that method is used and still returns an error – MikaAK Aug 02 '13 at 05:38
  • 1
    You're using `(Suits)suitVal` but the enum you posted is actually named `Suit`. – p.s.w.g Aug 02 '13 at 05: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. – rutter Aug 02 '13 at 05: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?) – MikaAK Aug 02 '13 at 05:44
  • @user2434321 there are quite a few mistakes in your code. See my answer below – Ehsan Aug 02 '13 at 05:52
  • when you run the above code,at first it is going to throw **ArrayIndexOutofBoundsException**..Change the initial value of the loops.. – Naren Aug 02 '13 at 06:00
  • Solved by using Suits.Suit as it is a seperate class. Although my question now is how can i make it so i just use Suit to explicitly cast? – MikaAK Aug 02 '13 at 06:17
  • @user2434321 i have updated and verified my answer and it is working. you can use that – Ehsan Aug 02 '13 at 06:21

4 Answers4

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]
    ...
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • -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 Aug 02 '13 at 06:22
  • @Ehsan Ullah: Thank you, I've corrected index within cards[…] – Dmitry Bychenko Aug 02 '13 at 06:28
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;
}
Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • Intellisense is still directing me to using public readonly Suits suit; as oppsed to your answer. I should mention this is a class library. – MikaAK Aug 02 '13 at 06:14
  • 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. – Ehsan Aug 02 '13 at 06:25
  • This does not work when i had to do it it was Suits.Suit and Ranks.rank – MikaAK Aug 02 '13 at 07:02
  • @user2434321 please post your entire code. What is Suits? is it a class? what is Ranks? we cannot keep guessing – Ehsan Aug 02 '13 at 07:08
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.

Harris Yer
  • 281
  • 2
  • 10
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 
internals-in
  • 4,798
  • 2
  • 21
  • 38