0

I am trying to deal 5 cards to a Hand object that can contain 6 card objects. Ive written the hand class, the deck class and the card class.

The code deals cards to the Hand object which can contain Card objects, but i keep on getting the System.Reflection.TargetInvocationException in Presentation.dll exception.

//function to deal specified player the topmost cards from deck
//recieves an array of hands or Hand object
public void dealPlayerCardsFromDeck(Hand players, int numberOfCards )
{
    int j = 0;

    //loop to go through deck elements and to ensure the end of the player's hand isnt reached
    for (int i = 0; (i < deckLength) && (j <= numberOfCards); i++)
    {
       if (deck[i] != null)
       {
          players.hands[j].face = deck[i].face;
          players.hands[j].value = deck[i].value;
          deck[i] = null;
          j++;
       }
    }
}//end function

Here is the code that calls it in Main()

cardDeck.dealPlayerCardsFromDeck(players[0],5);

the "cardDeck" is an object of Class Deck

Deck cardDeck = new Deck();

Please note I am using c#

Longball27
  • 676
  • 1
  • 8
  • 29
Lxrd-AJ
  • 592
  • 7
  • 16

1 Answers1

0

I think the most logical explanation is some sort of index out or range issue in the loop when indexing the arrays using i or j. I recommend following the execution in a debugger to see if i or j is incremented past the size of the arrays

TGH
  • 38,769
  • 12
  • 102
  • 135
  • I did do that, the exception occurs at "players.hands[j].face = deck[i].face;" before i or j gets incremented – Lxrd-AJ Jul 15 '13 at 02:30