6

The error is caused in the for loop :

for (i = 0; i < hand.Length; i++)
{
   Console.WriteLine(hand[i]);
}

I am trying to store the values to be able to display them at a later time. The writeline is there to help me make sure the code actually works as I intend it to.

The rest of the code for reference: *edit: added a line of code

enum house //variable type for the card type
{
    Spades, Hearts, Clubs, Diamonds
}

enum cards //variable type for the cards
{
    Joker, Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King
}

class Program
{
    static void Main(string[] args)
    {

        Random rnd;
        Random rnd2;

        int i;
        int random;
        int random2;

        String[] hand;

        house randomhouse;
        cards randomcard;

        //all declared variables

        Console.WriteLine("Your hand is made up of :");

        for (i = 0; i <= 6; i++)//does everything in the {} until i is equal to 6
        {

            rnd2 = new Random();
            random2 = rnd2.Next(0, 14);
            randomcard = (cards)random2; //selecting a random card from joker to king

            if (randomcard > (int)cards.Joker) //if the random card isn't a joker
            {
                rnd = new Random();
                random = rnd.Next(0, 4);
                randomhouse = (house)random;//selects a random card type

                Console.WriteLine(randomcard + " of " + randomhouse); //outputs the name of the card
                System.Threading.Thread.Sleep(1000);//wait 1 second before getting the new card
            }

            else
            {
                Console.WriteLine(randomcard);//outputs "Joker"
                System.Threading.Thread.Sleep(1000);//wait 1 second before getting the new card
            }

            hand = new String[i];//making a new array value for every loop
            hand[i] = randomcard.ToString();//adding randomcard to the array*

        } 

        Console.Clear();

        for (i = 0; i < hand.Length; i++)
        {
            Console.WriteLine(hand[i]);
        }

        Console.ReadKey();
    }
}
ardox
  • 83
  • 1
  • 5
  • 1
    `hand[i] = randomcard`, perhaps? It's really not clear what should be there. – raina77ow Apr 02 '14 at 13:55
  • `hand = new String[i];` will completely overwrite the array each time you go through the loop, by the way (i.e. it creates an array of length 0, then replaces it with one of length 1, etc.). I'm not sure what you're trying to put in the array, but as is you could pull that line out after the `for` loop to achieve the same results (and get rid of your error). – Michelle Apr 02 '14 at 13:58
  • As a side note, you are always creating a new String array in your first for loop and you are not actually storing anything inside this new array. If you wanted to store an actual value you would have to use hand[i] = value; – Matthew Apr 02 '14 at 14:00

2 Answers2

8

The compiler can never be sure that hand is actually initialized. You should either initialize it earlier, or set it to null, so you bypass this compiler check.

So you could do this, but it is in fact bad practice! When changing your code you could end up with a NullReferenceException!

String[] hand = null;

You do know your code doesn't actually work, since you end up with one array in the end. I think you mean this:

hand = new String[6];

...

hand[i] = theValue;
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

Arrays in c# aren't dynamic. You need to manually resize them to add anything in them. Initialize your hand outside of the for loop. Since a starting hand is 6 cards big you can assign this.

string[] hand = new string[6];

If new cards are added you can use:

Array.Resize(ref hand, (i + 1));
HansMat
  • 11
  • 1