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?

Tarheel81
  • 13
  • 1
  • 7

1 Answers1

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;}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 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 Apr 20 '17 at 04:21
  • @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 `. – Ousmane D. Apr 20 '17 at 04:23
  • @Tarheel81hi, iam new but i what i thinking is why not using `get` `set` in global variable Class. http://stackoverflow.com/questions/5096926/what-is-the-get-set-syntax-in-c – chopperfield Apr 20 '17 at 04:35
  • @chopperfield the example above is no different to using both `get`/`set`, however, you can use it. see update. – Ousmane D. Apr 20 '17 at 04:37