Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Beginner
Mar 24, 1997

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 Object
objNext = frmMain.FirstChild
DO 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 Object
objNext = frmMain.FirstChild
DO 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.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date