Micro Focus QTP (UFT) Forums
Send an object name (followed by a number) to ChildObjects? - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: Send an object name (followed by a number) to ChildObjects? (/Thread-Send-an-object-name-followed-by-a-number-to-ChildObjects)



Send an object name (followed by a number) to ChildObjects? - MGMN - 09-23-2008

In a function I have created four objects called oLevel1, oLevel2, oLevel3, oLevel4. In each iteration/step of a For-Next loop I want to list the child objects of each of the objects. However I'm not sure how to do this.

When I try to send the following concatenation (of oLevel+number) to ChildObjects I get an error at the ChildObjects line:
Code:
Set oLevel1 = Description.Create()
...
For Iterator = 1 To 4  Step 1
   nLevel = 1
   Set List = parentFrame.ChildObjects(oLevel & nLevel) 'get childs from e.g. oLevel1 object
   ...
   nLevel = nLevel + 1
Next
...
Is there a way to send in an object called "oLevel1" to ChildObjects in the first iteration and "oLevel2" in the second iteration etc, i.e. concatenate "oLevel" with the nLevel number before sending it?

/MGMN


RE: Send an object name (followed by a number) to ChildObjects? - niranjan - 09-23-2008

One way to do would be by using a case statement. But, if you have more objects this way would become a pain. Since you only have 4 objects you can use this way.

Code:
Set oLevel1 = Description.Create()
...
For Iterator = 1 To 4  Step 1
      Select Case Iterator
             Case 1
                      Set List = parentFrame.ChildObjects(oLevel & nLevel) 'get childs from e.g. oLevel1 object
                       ...
             Case 2
                       Set List = parentFrame.ChildObjects(oLevel & nLevel) 'get childs from e.g. oLevel2 object
                       ...
             Case 3
                       Set List = parentFrame.ChildObjects(oLevel & nLevel) 'get childs from e.g. oLevel3 object
                       ...
             Case Else
                       Set List = parentFrame.ChildObjects(oLevel & nLevel) 'get childs from e.g. oLevel4 object
                       ...
       End Select
Next



RE: Send an object name (followed by a number) to ChildObjects? - MGMN - 09-24-2008

[EDIT: I have thanks to niranjan's tips about the Case statement (see the answer above) found a solution that works, but my question still remains and can make it easier to handle bigger iterations.]

Yes, a Case statement is another (maybe smarter) solution, but my problem isn't the For-Next loop. My problem is that I don't know how to concatenate the string "oLevel" with iteration number "nLevel" into "oLevel1" so it is sent as an object to ChildObjects.

It doesn't seem to be possible to concatenate "oLevel & nLevel" into an object like this:
Code:
ChildObjects(oLevel & nLevel)
When looking at this code now it seems logic that it didn't work since the "oLevel" variable doesn't actually exist.

However I have tried one more solution and that is to set the letters in "oLevel" to a string and concatenate this string with nLevel into "oLevel1" before sending it to ChildObjects. This solution was something like this:
Code:
nLevel = 1
sLevelString = "oLevel" & nLevel
Set List = parentFrame.ChildObjects(sLevelString) 'get childs from e.g. oLevel1 object
But this didn't work either. Unfortunately I'm not at my QTP machine now to try it out and it was a couple of hours since I tried it so I'm not 100% sure about the code.

But the question remains; how can I concatenate a number and a string in order to send the object "oLevel1"?


RE: Send an object name (followed by a number) to ChildObjects? - niranjan - 09-24-2008

I agree with you. Your solution should work. May be try "+" instead of "&"


RE: Send an object name (followed by a number) to ChildObjects? - mayno224 - 09-30-2008

Try using the eval function.

Code:
Eval (Set List = parentFrame.ChildObjects(oLevel & nLevel))