cancel
Showing results for 
Search instead for 
Did you mean: 

Object Exists problem at the time of object declared

Highlighted
Regular Contributor

Object Exists problem at the time of object declared

Set obj = ..................
If (obj.Visible = true And obj.Exists= True)

 

    ........

Else

     ''''''''''

End IF

 

In the above code, i got error on 1st line also where object is only specify..... i think , it should be in Exists properties always by default.
What you think about it

1 REPLY 1
Highlighted
Community Hero

Hi,

 

> i got error on 1st line

Not sure what line is first...

My guess is that the first one is the line with the IF condition.

If my guess is correct, then this is expected behavior of VBScript.

VBScript does not have short logical expressions evaluation circuit and always executes the complete one. This means that if the given object does not exist then obj.Visible clause will be evaluated (because it is first in the condition and because of the full evaluation) and will post an error because there is no Visible property in the non-existing object.

The correct implementation for VBScript is like this:

If (obj.Exists) Then
  If (obj.Visible) Then
    ...
  Else
    ' object is not visible but exists
  End If
Else
  ' object does not exist
End If

Note 1:

For the languages that support short evaluation, the correct implementation is to specify .Exists check to be the first one in the condition:

if (obj.Exists && obj.Visible) then
  // object exists and is visible
else
  // there will be no error if the object does not exist because .Visible will not be evaluated

Note 2: It is not recommended to explicitly compare boolean values to either True or False in the if() clauses.

 

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
New Here?
Join us and watch the welcome video:
Announcements
Join TechCorner Challenge!
Want a fun and easy way to learn TestComplete? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get properties of a web page element

See replies!

Compare images using the Region Checkpoint

See replies!

Compare HTML table with Excel file and correct data in Excel file

Participate!

How to execute remote test and obtain results via Test Runner REST API

Participate!
Top Kudoed Authors