15

这大概只是一厢情愿...

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

4

3 回答 3

16

这是我的解决方案,其工作原理相同,但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 
于 2009-09-11T22:43:55.703 回答
6

这是一种有点笨拙的方法,因为它依赖于设置“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

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

于 2009-05-28T15:27:23.473 回答
0

提供的答案是正确的,我只是做了一个简单的修改,Err.Clear根据提供的答案添加和压缩函数。

我还将函数重命名为更合适的名称,以反映它可用于检查函数和子例程是否存在的事实。

Function ProcExists(ByVal ProcName)

 On Error Resume Next
  Set ProcName = GetRef(ProcName)
  ProcExists   = (Err.Number = 0)
  Err.Clear
 On Error GoTo 0

End Function
于 2021-02-17T11:06:09.677 回答