Proper way to call an instance of a variable.

Digimstr

Member
Joined
Sep 26, 2014
Messages
7
Programming Experience
Beginner
I working on a program that I roll some dice and want to call a specific picture box by what die is rolled.

the picture boxes are named consistently pbDie1, pbDie2, etc.

What I am trying to figure out is if the die roll is 1 then it should show pbDie1. I am stumped on how to doe this as the die roll is set to int so not sure how to assign the window according to the roll of the die.

Thanks
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,150
Location
Sydney, Australia
Programming Experience
10+
Firstly, there is no proper way to call an instance of a variable because variables don't have instances and and you don't call a variable. What you want to know is how to select one of a number of objects based on a number. The obvious solution is to put the objects into an array and then use the number as an index into that array. You would create an array with six elements and put the six PictureBoxes into it. When you "roll your die", you'll get a number in the range 1 to 6. Subtract 1 from that and get the array element at that index, as the array has elements at indexes 0 to 5.
 
Top Bottom