ASP的IsNothing Function

ASP的IsNothing Function

VBScript中所有變數都是Variant,所以物件有可能是Empty、Nothing、Null…

所以在判斷變數無法像Asp.Net直接用IsNothing的Function,後來上網找到一個,就在此備份一下。


Function IsNothing(Value)

    Dim vTest
    Dim iArrayStart
    Dim iCtr, nDim, nRows, nCols, x, y
    Dim objLog, strErrMsg
    Dim bFlag :    bFlag = false
    
    If IsEmpty(Value) Then
    IsNothing = True
    Exit Function
    End If
    
    If IsNull(Value) Then
    IsNothing = True
    Exit Function
    End If
    
    If VarType(Value) = vbString Then
    If Value = "" Then
    IsNothing = True
    Exit Function
    End If
    End If
    
    If IsNumeric(Value) Then
    If Value = 0 Then
    IsNothing = True
    Exit Function
    End If
    End If
    
    If IsObject(Value) Then
    If Value Is Nothing Then
    IsNothing = True
    Exit Function
    End If
    End If
    
    'Check for arrays
    
    If IsArray(Value) Then
    nDim = NoDim(Value)
    'Handle mutli dim arrays
    If nDim = 0 then
    IsNothing = true
    Exit Function
    Elseif nDim = 1 then 'check single dim array
    On Error Resume Next
    'Handle Single dim arrays
    vTest = Value(0)
    iArrayStart = IIf(Err.Number = 0, 0, 1)
    Err.Clear
    On Error GoTo 0
    For iCtr = iArrayStart To UBound(Value)
    If Not IsNothing(Value(iCtr)) Then
    'Has something in it
    bFlag = True
    Exit For
    End If
    Next
    
    IsNothing = Not bFlag
    Exit Function
    Elseif nDim = 2 then
    nRows = Ubound(Value, 2)+1
    nCols = Ubound(Value, 1)+1
    
    for x = 0 to nRows - 1
    for y = 0 to nCols - 1
    If not IsNothing(Value(y,x)) then
    bFlag = True
    Exit For
    End if
    next
    if (bFlag) then
    Exit For
    end if
    next
    IsNothing = Not bFlag
    Exit Function
    End if
    End If
    
    IsNothing = False

End Function

'** will retun the number of dimensions of an array
Function NoDim(arr)

    dim n, mynextArray
    
    On Error Resume Next
    For n = 1 to 60
    mynextArray = UBound(arr, n)
    If err.number > 0 Then
    n = n - 1
    Exit For
    End if
    Next
    On Error Goto 0
    
    NoDim = n

End Function

參考資料:Empty, Nothing, and Null. How do you feel today?

Hi, 

亂馬客Blog已移到了 「亂馬客​ : Re:從零開始的軟體開發生活

請大家繼續支持 ^_^