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

发布于 2024-07-22 10:03:22 字数 63 浏览 12 评论 0原文

这可能只是一厢情愿...

有没有办法在调用 ASP/VBScript 函数之前检查它是否已定义?

This is probably just wishful thinking...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌若浮生 2024-07-29 10:03:22

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

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 

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 
饮湿 2024-07-29 10:03:22

这是一种有点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

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

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.

女皇必胜 2024-07-29 10:03:22

提供的答案是正确的,我只是通过添加 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

The provided answers are correct, I just made a simple modification by adding Err.Clear and compacted the function based on the answers provided.

I also renamed the function to a more appropriate name to reflect the fact that it can be used to check the existence of both functions and subroutines.

Function ProcExists(ByVal ProcName)

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

End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文