Forum Discussion

kaiiii's avatar
kaiiii
Regular Contributor
6 years ago

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

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    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.