devxlogo

Enumerating instances

Enumerating instances

Question:
I want to use many instances of a class in my form, but can’t seem to figure out an easy way to check on the status of all the instances at once.

Answer:
There are two separate problems for you to deal with. The first is the need to enumerate through the objects in your form. You can accomplish this by using a form property, FirstChild, and an object level method, NextControl().

Each form has a FirstChild property, which is a reference to the first object in the internal list of objects that belong to a form. You can create an object variable and set it to reference this object. To enumerate through the objects on a form, you can set up a DO loop to iterate through the objects on the form with the NextControl() method of the current object, which returns either a reference to the next control in the form or a null value. The code for this procedure for a form named frmMain looks like this:

DIM objNext AS ObjectobjNext = frmMain.FirstChildDO UNTIL ISNULL(objNext)        conditional code        objNext = objNext.NextControl()LOOP

The enumeration described above will cycle through objects one level below the containing form. This means it will pick up the instance of a class on the form but not any objects contained in the form.

You can identify instances of a class with the HasProperty operator, which is new in OPO version 1.0.16. The HasProperty operator returns True if the object on the left has the property on the right and False if it doesn’t. If the class you were looking for has a property called “udpRequired”, the code for the above loop would look something like this:

DIM objNext AS ObjectobjNext = frmMain.FirstChildDO UNTIL ISNULL(objNext)        IF objNext HasProperty udpRequired THEN                conditional code        END IF        objNext = objNext.NextControl()LOOP

For instance, you can use the conditional code to test for a failure condition. If the failure condition is true, you can use EXIT DO to leave the loop. You can easily determine if any of the class instances caused a failure by checking the value of objNext after the loop. If there were no failures and the loop ended naturally, objNext will be null, otherwise, objNext will contain a reference to the first instance of the class that failed.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist