-->

错误检查在VBScript NULL(Error checking for NULL in VBSc

2019-07-18 16:06发布

我有一个经典的ASP页面下面的VBScript:

function getMagicLink(fromWhere, provider)
    dim url 
    url = "magic.asp?fromwhere=" & fromWhere
    If Not provider is Nothing Then ' Error occurs here
        url = url & "&provider=" & provider 
    End if
    getMagicLink = "<a target='_blank' href='" & url & "'>" & number & "</a>"
end function

我不断收到关于这行“对象需要”错误信使If Not provider Is Nothing Then

要么值为NULL,或者它不为空,那么为什么我收到此错误?

编辑:当我调用对象,我通过在任一NULL,或我通过在一个字符串。

Answer 1:

从你的代码,它看起来像provider是一个变体或其他一些变量,而不是一个对象。

Is Nothing仅作对象,但后来你说这是一个值,该值应为NULL或NOT NULL,这将通过以下方式处理IsNull

尝试使用:

If Not IsNull(provider) Then 
    url = url & "&provider=" & provider 
End if

或者,如果还是不行,请尝试:

If provider <> "" Then 
    url = url & "&provider=" & provider 
End if


Answer 2:

我看到很多困惑中的注释。 NullIsNull()vbNull主要用于数据库处理和通常在VBScript不使用。 如果没有明确的调用对象/数据的文件中指出,不使用它。

测试如果变量未初始化,使用IsEmpty() 测试一个变量是未初始化,或者包含""上,测试""Empty 。 为了测试一个变量是一个对象,使用IsObject并看看这个对象没有参考测试上Is Nothing

在你的情况,你首先要测试如果变量是一个对象,然后看看是否变量是Nothing的,因为如果它不是一个目标,你会得到错误,当你在测试“需要的对象” Nothing

片段混搭在您的代码:

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


Answer 3:

我只是添加一个空白(“”)为变量的结束和做比较。 当变量为空像下面的东西应该甚至不工作。 您还可以修剪的变量只是在空间的情况下。

If provider & "" <> "" Then 
    url = url & "&provider=" & provider 
End if


文章来源: Error checking for NULL in VBScript