gpt4 book ai didi

vbscript - 检查子函数或函数是否存在

转载 作者:行者123 更新时间:2023-12-03 06:30:08 25 4
gpt4 key购买 nike

有没有办法检查子或函数是否存在?

sub mySub()
'some code
end sub

类似于if exit(mySub)

最佳答案

更新:

所以我知道有一个更好的方法可以做到这一点,它使用 GetRef()

Function Exist(procName)
On Error Resume Next
Dim proc: Set proc = GetRef(procName)
Exist = (Not proc Is Nothing)
End Function

Function Hello()
WScript.Echo "Hello Ran"
End Function

If Exist("test") Then 'Returns False (0)
WScript.Echo "Test Exists"
Else
WScript.Echo "Test Doesn't Exist"
End If

If Exist("Hello") Then 'Returns True (-1)
WScript.Echo "Hello Exists"
Else
WScript.Echo "Hello Doesn't Exist"
End If

输出

Test Doesn't ExistHello Exists

There is nothing built into VBScript to do this but you can build something using On Error Resume Next and ExecuteGlobal().

Function Exist(procName)
On Error Resume Next
ExecuteGlobal "Call " & procName & "()"
Exists = (Err.Number = 0)
End Function

Function Hello()
WScript.Echo "Hello Ran"
End Function

If Exist("test") Then 'Returns False (0)
WScript.Echo "Test Exists"
Else
WScript.Echo "Test Doesn't Exist"
End If

If Exist("hello") Then 'Returns True (-1)
WScript.Echo "Test Exists"
Else
WScript.Echo "Test Doesn't Exist"
End If

输出

Test Doesn't ExistHello RanTest Doesn't Exist

这种方法的缺点是它实际上会运行该过程(如果存在)。

关于vbscript - 检查子函数或函数是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35919272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com