有没有办法检查是否定义了VBScript函数?

时间:2022-06-16 05:10:17

This is probably just wishful thinking...

这可能只是一厢情愿的想法......

Is there any way to check to see if an ASP/VBScript function is defined before calling it?

有什么方法可以在调用之前检查是否定义了ASP / VBScript函数?

2 个解决方案

#1


6  

It's a slightly hacky way to do it as it relies on having set "On Error Resume Next", but you could do something like this:

这是一个有点hacky的方式,因为它依赖于设置“On Error Resume Next”,但你可以这样做:

On Error Resume Next
Dim objRef1, objRef2
Set objRef1 = GetRef("DoStuff1")
If objRef1 Is Nothing Then
    Call objRef1
Else
    MsgBox "DoStuff1 is not defined!"
End If

Set objRef2 = GetRef("DoStuff2")
If objRef2 Is Nothing Then
    MsgBox "DoStuff2 is not defined!"
Else
    Call objRef2
End If

Sub DoStuff1
    MsgBox "DoStuff1!"
End Sub

The call to GetRef will generate an exception if the sub or function you're trying to get a pointer to does not exist (as is the case here with DoStuff2). You can then check if the reference was set as expected.

如果您尝试获取指针的子或函数不存在,则对GetRef的调用将生成异常(如此处使用DoStuff2的情况)。然后,您可以检查引用是否按预期设置。

#2


15  

Here is my solution which works on the same principle, but the hacky-ness is pretty self-contained:

这是我的解决方案,它的工作原理相同,但hacky-ness非常独立:

Function FunctionExists( func_name )
    FunctionExists = False 

    On Error Resume Next

    Dim f : Set f = GetRef(func_name)

    If Err.number = 0 Then
        FunctionExists = True
    End If  
    On Error GoTo 0

End Function 

#1


6  

It's a slightly hacky way to do it as it relies on having set "On Error Resume Next", but you could do something like this:

这是一个有点hacky的方式,因为它依赖于设置“On Error Resume Next”,但你可以这样做:

On Error Resume Next
Dim objRef1, objRef2
Set objRef1 = GetRef("DoStuff1")
If objRef1 Is Nothing Then
    Call objRef1
Else
    MsgBox "DoStuff1 is not defined!"
End If

Set objRef2 = GetRef("DoStuff2")
If objRef2 Is Nothing Then
    MsgBox "DoStuff2 is not defined!"
Else
    Call objRef2
End If

Sub DoStuff1
    MsgBox "DoStuff1!"
End Sub

The call to GetRef will generate an exception if the sub or function you're trying to get a pointer to does not exist (as is the case here with DoStuff2). You can then check if the reference was set as expected.

如果您尝试获取指针的子或函数不存在,则对GetRef的调用将生成异常(如此处使用DoStuff2的情况)。然后,您可以检查引用是否按预期设置。

#2


15  

Here is my solution which works on the same principle, but the hacky-ness is pretty self-contained:

这是我的解决方案,它的工作原理相同,但hacky-ness非常独立:

Function FunctionExists( func_name )
    FunctionExists = False 

    On Error Resume Next

    Dim f : Set f = GetRef(func_name)

    If Err.number = 0 Then
        FunctionExists = True
    End If  
    On Error GoTo 0

End Function