Forum Discussion

blearyeye's avatar
blearyeye
Contributor
14 years ago

Checking for object existence

I'm trying to figure out how to check whether an object exists. There's a lot of stuff on the TC site about this, but I can't seem to find what addresses my fairly simple need.



I want to check whether Aliases.STUV1.frmParent.frmMainMenu.panel2.lblUser exists or not (I'm using Javascript in TC for a .NET 4.0 WinForms application). WaitWindow doesn't seem like the right thing to use. I know there are other WaitNNN methods but don't know which to use.



tia, Bill

3 Replies

  • Hi Bill,


    You need to use the WaitAliasChild method:



    var obj = Aliases.STUV1.frmParent.frmMainMenu.panel2.WaitAliasChild("lblUser");

    if (obj.Exists)

      Log.Message("It exists!")

    else

      Log.Message("It does not exist!")

  • Sorry for the delay, was distracted by other stuff ...



    I ran your suggested code on a form whose path did not include frmMainMenu. The result was an exception:



         The Aliases.STUV1.frmParent.frmMainMenu.panel2 object does not have a child with the name "lblUser".



    The problem I'm having is that I want to test the existence of an object, but get an exception if any of the objects in its path do not exist.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    You need to, then, make sure you do appropriate checks along the line as well...  If there is the possibility that, when you get to looking for lblUser, some of the other objects may need to be waited for, you should also use the WaitAliasChild there, too.



    Additionally, you might want to verify your mapping criteria to make sure you're properly identifying the objects, that they aren't returning the wrong object.



    Also, the WaitAliasChild is only valid from an object that actually has that child.  so, if you don't have that frmMainForm and you try calling it in your path, you will inevitably get the error.  



    Now, if you want to FIND the child anywhere in your hierarchy and return whether or not it is find, use FindChild.  so, I'd change your code like so:



    , 10)

    if (obj.Exists)

      Log.Message("It exists!")

    else

      Log.Message("It does not exist!")




    This is assuming that the list of properties and their corresponding values are correct.