‎2007 Jun 25 11:55 AM
hi there
how to restrict number of objects to be created for a class??
‎2007 Jun 25 12:02 PM
Hi Tuborg,
In the constructor mthod, increment a static attribute, keep a check on its value and display a message accordingly.
Award points if found useful.
Regards
Indrajit.
‎2007 Jun 25 12:06 PM
‎2007 Jun 25 12:10 PM
Hi Indrajit, Tuborg,
Is this really going to restrict the number of objects created?! I dont think so! When the constructor is called, the object is going to instantiated.
A better method i would think is keep a static attribute for the class say class A, to keep a count of the number of objects and increment in the contructor. Also, make the constructor private. In a friend class, say class B, instatiate the class A and always check the count variable and do not instantiate the class if it exceeds the max desired value.
Reward points if helpful.
Regards,
Renuka.
‎2007 Jun 25 12:16 PM
Hi,
Why do we need a friend class?
Increment static variable in constructor and check its value before instantiating the class.
Tuborg,
You can change CONSTRUCTOR method of the global class in SE24 and do the check in your program.
Regards
Indrajit.
‎2007 Jun 25 12:18 PM
Hi Tuborg,
For standard classes, its really not possible to restrict the number of objects strictly as they might not even have a static attribute to count the number of objects instantiated. What you can do is keep a variable locally in ur program and before you instantiate the class, check if value is greater than equal to the max value desired. Increment the value after instantiation.
Reward points if helpful.
Regards
Renuka.
‎2007 Jun 25 12:21 PM
Hi Tuborg,
Consider the following two cases.
<u><b>1. For one program context you need to restrict the number of instances of a global class</b></u>
Possible. Use the method I had suggested earlier.
<u><b>2. Irrespective of program context, you need to restrict the number of instances of a global class.</b></u>
For this case you can try using PERSISTENT classes to store the objects of the class persistently.
Award points if found useful.
Regards
Indrajit
‎2007 Jun 27 3:10 PM