C'è un modo per verificare se si definisce una funzione VBScript?
-
06-09-2019 - |
Domanda
Questo è probabilmente solo un pio desiderio ...
C'è un modo per verificare se una funzione ASP / VBScript è definito prima di chiamare?
Soluzione
E 'un modo un po' hacky per farlo in quanto si basa su aver impostato "On Error Resume Next", ma si potrebbe fare qualcosa di simile:
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
La chiamata alla GetRef genererà un'eccezione se il sub o funzione si sta cercando di ottenere un puntatore non esiste (come è il caso qui con DoStuff2). È quindi possibile controllare se il riferimento è stato impostato come previsto.
Altri suggerimenti
Ecco la mia soluzione che funziona sullo stesso principio, ma il hacky-ness è abbastanza autosufficiente:
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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow