1

I'm creating a Pokemon-type game in Visual Studio. I'm working on the Inventory form right now.

My question is can I use a public count variable as a way to keep track of the inventory?

Setting it:

public int healthPotionCount = 0;

Then if a player encounters a healthPotion increase it's value

if(picPlayer.Bounds.IntersectsWith(hPotion.Bounds)
      {
          healthPotionCount++;
      }

Finally using that same public variable to determine whether to show it in the inventory:

if(healthPotionCount > 0)
   {
       picBox.BackgroundImage = (the health potion image);
       lblQuantity.Text = ""+healthPotionCount; 
   }

With this, my issue is that I have a lot of forms across my solution and I want this variable to be accessible by all forms. Is this logic too awkward to work?

0

1 Answer 1

1

With this, my issue is that I have a lot of forms across my solution and I want this variable to be accessible by all forms.

you could make it static meaning it belongs to the class rather than a particular form object hence you can access it within multiple forms.

public static int healthPotionCount { get; set;}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, so in my GlobalVariable class it reads: class healthPotion { public static int healthPotionCount = 0; } But I am still unable to call it another form using MessageBox.Show(" " +healthPotionCount);
@Tarheel81 in order to access it in another form you need to specify the name of the form then the variable i.e if you want to access it from form2 you can do Form1.healthPotionCount .
@Tarheel81hi, iam new but i what i thinking is why not using get set in global variable Class. stackoverflow.com/questions/5096926/…
@chopperfield the example above is no different to using both get/set, however, you can use it. see update.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.