Random numbers working throughout classes

Category: c# language

Question

Damian G a c sharp prgrammer on Sun, 30 Sep 2012 23:01:26


Hey guys,

I am making a card game and i am trying to use a random number to reference a card in the deck.  The issue is that if I go other classes, the value of my random number becomes 0 and results in a runtime error.  Can you please help? I'll send some code to help you (note: it is all in order):

public void Form1_Load(object sender, EventArgs e)
        {
//Initialising random and random integer
Random random5 = new Random();
            int randomNumberHand1_P1 = random.Next(1, (scopaDeck.Images.Count + 1));
//Dealing card (Player 1's hand, card 1)
            hand_P1.Images.Add(scopaDeck.Images[randomNumberHand1_P1]);
            scopaDeck.Images.RemoveAt(randomNumberHand1_P1);
            pbHand1Card_P1.BackgroundImage = scopaDeck.Images[randomNumberHand1_P1];
}
}
//To put the card down under certain conditions
if (pbTable5.BorderStyle == BorderStyle.Fixed3D && pbTable5.BackgroundImage == null && pbHand1Card_P1.BorderStyle == BorderStyle.Fixed3D &&
                     pbTable1.BorderStyle == BorderStyle.None && pbTable2.BorderStyle == BorderStyle.None && pbTable3.BorderStyle == BorderStyle.None && pbTable4.BorderStyle == BorderStyle.None &&
                     pbTable6.BorderStyle == BorderStyle.None && pbTable7.BorderStyle == BorderStyle.None)
                {
                    //Put the card on the table
                    pbTable5.BackgroundImage = pbHand1Card_P1.BackgroundImage;
                    Globals.tableCards.Images.Add(Globals.hand_P1.Images[Globals.randomNumberHand1_P1]);
                    //Take the card out of player 1’s hand
                    pbHand1Card_P1.BackgroundImage = null;
                    Globals.hand_P1.Images.RemoveAt(Globals.randomNumberHand1_P1);
                }
}

public static class Globals
        {
            //To access some objects across classes
//Randoms
            public static Random random5 { get; set; }
//Integers
            public static int randomNumberHand1_P1 { get; set; }
}


Replies

Damian G a c sharp prgrammer on Sun, 30 Sep 2012 23:03:19


The second piece of code is in a public class that describes what to do on the press of the Fold (putting a card down on the table) button. 

Ante Meridian on Sun, 30 Sep 2012 23:20:29


You are declaring a local integer instead of setting the global class's value. Hence it's not available to another class.

public void Form1_Load(object sender, EventArgs e)
{
   //Initialising random and random integer

   //Use your Global class here, rather than declare local variables.
   Globals.random5 = new Random();
   Globals.randomNumberHand1_P1 = Globals.random5.Next(1, (scopaDeck.Images.Count + 1));
   //Dealing card (Player 1's hand, card 1)
   hand_P1.Images.Add(scopaDeck.Images[Globals.randomNumberHand1_P1]);
            scopaDeck.Images.RemoveAt(Globals.randomNumberHand1_P1);
            pbHand1Card_P1.BackgroundImage =    scopaDeck.Images[Globals.randomNumberHand1_P1];
   }
}




Damian G a c sharp prgrammer on Mon, 01 Oct 2012 02:43:12


That does work, thanks, but on runtime I get an error saying (as I did before), "Object reference not set to an instance of an object." 

Ante Meridian on Mon, 01 Oct 2012 03:19:23


Where in the code do you get this error?

Damian G a c sharp prgrammer on Mon, 01 Oct 2012 03:34:41


When I add the players card to the new imagelist (tablecards):

else if (pbTable5.BorderStyle == BorderStyle.Fixed3D && pbTable5.BackgroundImage == null && pbHand1Card_P1.BorderStyle == BorderStyle.Fixed3D &&
                     pbTable1.BorderStyle == BorderStyle.None && pbTable2.BorderStyle == BorderStyle.None && pbTable3.BorderStyle == BorderStyle.None && pbTable4.BorderStyle == BorderStyle.None &&
                     pbTable6.BorderStyle == BorderStyle.None && pbTable7.BorderStyle == BorderStyle.None)
                {
                    //Put the card on the table
                    pbTable5.BackgroundImage = pbHand1Card_P1.BackgroundImage;
                   //I got it on the line below this writing
Globals.tableCards.Images.Add(Globals.hand_P1.Images[Globals.randomNumberHand1_P1]);
                    //Take the card out of player 1’s hand
                    pbHand1Card_P1.BackgroundImage = null;
                    Globals.hand_P1.Images.RemoveAt(Globals.randomNumberHand1_P1);
                }

Ante Meridian on Mon, 01 Oct 2012 04:04:23


I assume that you mean this line...

Globals.tableCards.Images.Add(Globals.hand_P1.Images[Globals.randomNumberHand1_P1]);

This suggests that one or more of the following haven't been "new"ed beforehand...

Globals.hand_P1

Globals.hand_P1.Images

Globals.tableCards

Globals.tableCards.Images

You need to make sure that all of these things have been initialised, probably at the same place that you initialise the random integer.

Damian G a c sharp prgrammer on Mon, 01 Oct 2012 04:28:15


That worked! Thankyou very much, much appreciated!

Damian G a c sharp prgrammer on Mon, 01 Oct 2012 21:16:43


Actually it didn't work, the problem still exists

Ante Meridian on Mon, 01 Oct 2012 23:25:21


You will need to show us your code that you have so far and be specific about the error you are getting and where.

Just as a general explanation, the error "Object reference not set to an instance of an object" means that you have done something like this...

string myString;
int length = myString.Length;

In the code above, myString is null because it hasn't been set to anything, so trying to access one of its properties as in the second line will cause a crash. This is the sort of thing that you are looking for - a variable that has been declared but not instantiated before you are trying to use it.

Damian G a c sharp prgrammer on Tue, 02 Oct 2012 01:35:19


Ok thanks I'll chack for that. 

Damian G a c sharp prgrammer on Thu, 04 Oct 2012 07:06:34


It's okay, I've sorted out the problem, thanks for the advice.