Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

controlling objects

Former Member
0 Likes
645

hi there

how to restrict number of objects to be created for a class??

7 REPLIES 7
Read only

Former Member
0 Likes
624

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.

Read only

0 Likes
624

points awarded.

but how can we do it for standard classes??

Read only

0 Likes
624

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.

Read only

0 Likes
624

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.

Read only

0 Likes
624

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.

Read only

0 Likes
624

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

Read only

Former Member
0 Likes
624

thanks to all. points awarded