Is there any way to check to see if a VBScript function is defined?

為{幸葍}努か 提交于 2019-12-05 12:48:28

问题


This is probably just wishful thinking...

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


回答1:


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:

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.




回答2:


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

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 


来源:https://stackoverflow.com/questions/921364/is-there-any-way-to-check-to-see-if-a-vbscript-function-is-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!