Error checking for NULL in VBScript

后端 未结 3 1304
死守一世寂寞
死守一世寂寞 2020-12-05 18:02

I have the following VBScript in a Classic ASP page:

function getMagicLink(fromWhere, provider)
    dim url 
    url = \"magic.asp?fromwhere=\" & fromWhe         


        
3条回答
  •  我在风中等你
    2020-12-05 18:14

    I see lots of confusion in the comments. Null, IsNull() and vbNull are mainly used for database handling and normally not used in VBScript. If it is not explicitly stated in the documentation of the calling object/data, do not use it.

    To test if a variable is uninitialized, use IsEmpty(). To test if a variable is uninitialized or contains "", test on "" or Empty. To test if a variable is an object, use IsObject and to see if this object has no reference test on Is Nothing.

    In your case, you first want to test if the variable is an object, and then see if that variable is Nothing, because if it isn't an object, you get the "Object Required" error when you test on Nothing.

    snippet to mix and match in your code:

    If IsObject(provider) Then
        If Not provider Is Nothing Then
            ' Code to handle a NOT empty object / valid reference
        Else
            ' Code to handle an empty object / null reference
        End If
    Else
        If IsEmpty(provider) Then
            ' Code to handle a not initialized variable or a variable explicitly set to empty
        ElseIf provider = "" Then
            ' Code to handle an empty variable (but initialized and set to "")
        Else
            ' Code to handle handle a filled variable
        End If
    End If
    

提交回复
热议问题