-4

Hello people of StackOverflow. I am doing an application that randomize a set of 5 cards with 2 different arrays. 1 array is for the number of the card(Ace,king,queen,etc...) and the other array is for the type of card(spade,etc..)

What I am trying to do is to insert a random stringin the set of 5 cards using a for loop and then assigning the randomized string to the array I will be using to check if I have a pair.

Here is my code. The error I encounter is that the tbValuer and tbEnseigne are not assigned.

{
  string[] tbValeur   ;
  string[] tbEnseigne ;
  string[] tbNumero   =
    { "As"    , "Deux" , "Trois" , "Quatre" , "Cinq" ,
      "Six"   , "Sept" , "Huit"  , "Neuf"   , "Dix"  ,
      "Valet" , "Dame" , "Roi"
    };
  string[] tbCouleur  =
    { "Carreau" ,
      "Coeur"   ,
      "Trèfle"  ,
      "Pique"
    };
  Random rnd = new Random();

  for (int i = 0; i <=4; i++)
  {                
    tbValeur[i]   = tbNumero[   rnd.Next(0,12) ] ;
    tbEnseigne[i] = tbEnseigne[ rnd.Next(0,3)  ] ;
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
ThallForms
  • 87
  • 1
  • 7
  • 1
    You didn't even post a complete code block... also you have to specify the length of your string array, you are likely getting an argument out of range exception (that is if you ever instantiated the arrays) – eddie_cat Sep 09 '14 at 20:53
  • 1
    Try assigning to an actually instantiated array. I'm surprised that even compiles... – BradleyDotNET Sep 09 '14 at 20:53
  • You should be getting either compile time error/warning or null reference exception. In any case it is already covered in linked duplicate. For future questions please provide exact error message as part of the post. – Alexei Levenkov Sep 09 '14 at 21:28

1 Answers1

1

tbValeur and tbEnseigne are just variables that you've declared. You can't set their values because you haven't actually created a new array and assigned it variables.

you can initialize empty arrays like this.

int someInt = 5;
string[] tbValeur = new string[someInt];
string[] tbEnseigne = new string[5];

If you want more flexibility, you can use a List