Okay so, I'm programming a Yachzee game, and well, it's not working perticulary well.
When I click the button "Roll" this code is started.
int rand1 = rand()%6+1;
int rand2 = rand()%6+1;
int rand3 = rand()%6+1;
int rand4 = rand()%6+1;
int rand5 = rand()%6+1;
Dice^ t1 = gcnew Dice (rand1);
Dice^ t2 = gcnew Dice (rand2);
Dice^ t3 = gcnew Dice (rand3);
Dice^ t4 = gcnew Dice (rand4);
Dice^ t5 = gcnew Dice (rand5);
It creates five seperate random numbers and send them to my Dice.h as five seperate objects.
This is the code in Dice.h
using namespace System::Windows::Forms;
ref class Dice {
public:
Dice (int rand)
{
this->rand = rand;
createPictureBox();
}
private:
int rand;
PictureBox^ p;
public:
void createPictureBox()
{
//PictureBox^ p = gcnew PictureBox();
p->Size = System::Drawing::Size(91, 85);
if ( rand == 1 )
p->ImageLocation = "..\\Bilder\\dice_face_1.png";
else if ( rand == 2 )
p->ImageLocation = "..\\Bilder\\dice_face_2.png";
else if ( rand == 3 )
p->ImageLocation = "..\\Bilder\\dice_face_3.png";
else if ( rand == 4 )
p->ImageLocation = "..\\Bilder\\dice_face_4.png";
else if ( rand == 5 )
p->ImageLocation = "..\\Bilder\\dice_face_5.png";
else
p->ImageLocation = "..\\Bilder\\dice_face_6.png";
p->SizeMode = PictureBoxSizeMode::StretchImage;
}
public:
PictureBox^ getPictureBox()
{
return p;
}
int getRand()
{
return rand;
}
};
As it is now, the program breaks with an arrow pointing to the row which says
p->ImageLocation = "..\\Bilder\\dice_face_1.png";
And if I move the row which says
p->Size = System::Drawing::Size(91, 85);
under the else, where the row changing the SizeMode is it will break with a arrow pointing to the if, else if, or else which have the number corresponding to the value of rand. And if I look below where it seems to show all the different values of variables it will show this
Name | Value | Type
_________________________________________________________________
this | 0x02b6e9a4 { rand=1 p=<undefined value> } | Dice^
Last thing to add is that it says the following in the break pop up
Additional information: Object reference not set to an instance of an object.