tags:

views:

351

answers:

3

The object I'm working on is instantiated in javascript, but used in VBSctipt. In one code path the variable M.DOM.IPt is defined and has a value, however, in the other it is not. I need to detect if it has been defined or not. I checked that M.DOM is defined and accessable in both code paths. Every test I have tried simply results in Error: Object doesn't support this property or method.

I have tried:

  • IsEmpty(M.DOM.IPt)
  • M.DOM.IPt is Nothing
  • isNull(M.DOM.IPt)

Is there any way to detect the variable isn't defined and avoid the error?

note: I can put On Error Resume Next in and it will simply ignore the error, but I actually need to detect it and conditionally do something about it.

A: 

Have you tried On Error Goto label?

Joseph Bui
That's not supported in VBScript--only On Error Resume Next
Mark Cidade
Well, that and On Error Goto 0.
EBGreen
+1  A: 
On Error Resume Next
Err.Clear
MyVariable=M.DOM.Ipt
If Err.Number<> 0 Then
    'error occured - Ipt not defined
    'do your processing here
Else
    'no error - Ipt is defined
    'do your processing here
End If
Arvo
This seems to the best and only solution. Seems like a bad hack, but I'll have to use it.
alumb
Actually marxidad offered more elegant solution, based on same approach - but thanks :)
Arvo
A: 
    Function SupportsMember(object, memberName)
      On Error Resume Next

      Dim x
      Eval("x = object."+memberName)

      If Err = 438 Then 
        SupportsMember = False
      Else 
        SupportsMember = True
      End If

      On Error Goto 0 'clears error
    End Function
Mark Cidade