2

I have a class named Button which creates buttons to be displayed on screen. I would like to create an array of information for each button but need to know how many buttons have been created. Is there an easy way to keep track of how many times a class has been instantiated? I have tried this and it doesn't seem to work ... I figured if you put the counter in the constructor it would add one each time, but it doesn't seem to work.

private int children = 0;

public Button(Vector2 position, Vector2 fontPos, Color buttonColor, 
              String buttonText, Boolean clickable, String spriteName)
{
    this.position = position;
    this.buttonColor = buttonColor;
    this.buttonText = buttonText;
    this.clickable = clickable;
    this.spriteName += spriteName;
    this.fontPos = fontPos;
    children++;
}
Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
  • possible duplicate of [how can i find out how many objects are created of a class in C#](http://stackoverflow.com/questions/2392075/how-can-i-find-out-how-many-objects-are-created-of-a-class-in-c-sharp) – Gul Ershad Jun 27 '15 at 17:53

1 Answers1

8

make it static

private static int children = 0;
Muaz Othman
  • 427
  • 4
  • 13