0

I have already received this problem a few times, i wonder why i can't use my index in the switch, i receive an error that the array[index] = null, any reason why?

// array of classes, im using public getters and setters to access the rank and cardcolor
Cardgame[] cardgame = new Cardgame[50];
int index = 0;
string CardColor = "";

...             

for (int i = 0; i < 4; i++)
{
    switch (i)
    {
        case 1: CardColor = "red";
            break;
        case 2: CardColor = "blue";
            break;
        case 3: CardColor = "diamond";
            break;
        case 4: CardColor = "candy!";
            break;
    }
    for (int x = 0; x <= 13; x++)
    {
        index++; 
     Cardgame[index].Color = CardColor;
        switch (x)
        {

            default: Cardgame[index].Number = x.ToString();
                break;
            case 11: Cardgame[index].Number = "Farmer";

                break;
            case 12: Cardgame[index].Number = "Queen";

                break;
            case 13: Cardgame[index].Number = "King";

                break;
        }
    }
4
  • 4
    new Cardgame[50] initializes a Cardgame array with 50 nulls. Are you creating 50 new Cardgames to fill the array after that? Commented Aug 12, 2013 at 23:03
  • for (int i = 0; i <= 4; i++) should be for (int i = 1; i <= 4; i++) Commented Aug 12, 2013 at 23:05
  • 2
    you missing Cardgame[index]= new CardGame();You have to create the object before u use it Commented Aug 12, 2013 at 23:07
  • thank you for your support Dan Hunex, i'm really grateful that you take your time to help someone who has a stupid beginner question. Commented Aug 12, 2013 at 23:18

3 Answers 3

3

Because you have an array of Cardgame objects. The default array initializer will set all objects to null. You have to do a cardgame[index] = new Cardgame() first.

Sign up to request clarification or add additional context in comments.

Comments

1
// first define the possible values
var suits = new [] {"red", "blue", "diamond", "candy"};
var ranks = new [] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "farmer", "queen", "king"}

//var ranks = Enumerable.Range(1, 10).Select(n => n.ToString())
//                      .Concat(new [] {"farmer", "queen", "king"})
//                      .ToArray();

// given the known values, you just iterate over every possible index
for(int i = 0; i < suits.Length * ranks.Length; i++)
{
    Cardgame[i] = new CardGame();
    Cardgame[i].CardColor = suits[i % suits.Length];
    Cardgame[i].Number = ranks[i / ranks.Length];
}

// or some "harder" LINQ    
var Cardgame = (from s in suits
               from r in ranks
               select new CardGame(s, r)).ToArray();

1 Comment

this is quite interesting, instead of using a nested loop, i could try it in another way.
0

You are iterating through the first loop 5 times and iterating through 14 cards each time... That is 70 cards.

Also, you are never initializing any of the cardgame objects.

you want to do:

for (int i = 0; i < 4; i++)
{
    ...
    for (int x = 0; x < 13; x++)
    {
        Cardgame[index] = new Cardgame();            
        ...
        index++;
    }
    ...
}

2 Comments

You need to change the second loop as well. Either by starting at 0 or ending at 12... which means you would want to change your switch statement as well.
Thank you very much i just started to learn about arrays and i'm still have some green behind my ears about what is classes. i thank you for your help. I hope this will help others as well, this might prevent others for looking for hours what might have gotten wrong in your loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.