Monday, 9 September 2013

Creating array of object in loop in C#?

Creating array of object in loop in C#?

So when i create an object of deck, an exception is shown >
"indexoutofrangeexception was unhandled"
Please help ####
deck.cs >>
public class Deck {
Card[] card = new Card[52];
const int NumOfCards = 52;
public Deck()
{
string[] symbol = { "Diamonds", "Clubs", "Hearts", "Spades"};
string[] rank = { "Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Jack", "Queen", "King"};
for (int i = 0; i < card.Length; ++i)
{
card[i] = new Card(symbol[i / 13], rank[i % 13]); /// this is
the line with problem shown in debug
}
Console.WriteLine(card.Length);
}
public void PrintDeck() {
foreach (Card c in card)
Console.WriteLine(c);
}
}

No comments:

Post a Comment