0

I want to declare a static variable and increment it every time object of that class is created. I am using C# and am new to it.

public class student
    {
        static int idcount;         /// for the number of objects created and to use it to assign lib nos
        public info biodata;               /// for making biadata easily available for both students and teachers
        record rec;

        public student(int i)           /////   ******* parameterised constructor for adding entry
        {
            rec = new record();
            biodata = new info();                //// new student record so everything is empty
            idcount++;                  /// new object created so incrementing static variable
            biodata.regno = idcount;
            rec.limit = 0;              /// indicating the number of books he has issued
        }

        public int getidcount()
        { return idcount; }
        public student()                /////local constructor for reading objects from file
        {
            rec = new record();
            biodata = new info();
            rec.limit = 0;
        }
/// methods 
}
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • 2
    did you find any issue? – Matías Fidemraizer Jun 27 '15 at 12:30
  • just create a property StudentCount {get;set;} and update it ++ everytime a new student is created. won't you save the student records in database? you can get the count from there – Rami Sarieddine Jun 27 '15 at 12:57
  • Side note: C# is not Java! Use proper capitalization and properties instead of get methods. – Chris Jun 27 '15 at 14:18
  • 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 18:11

0 Answers0