modargumenthelpers.bas

来自「VB 加密----------能够加密解密控件」· BAS 代码 · 共 488 行 · 第 1/2 页

BAS
488
字号
    
    ' Get our optional values.
    Dim Result As Long
    Result = GetOptionalLongPair(OptionalIndex, UpperBound, ReturnIndex, OptionalCount, UpperBound - LowerBound + 1, ReturnCount)
    If Result <> NO_ERROR Then
        GetOptionalArrayRangeReverse = Result
        Exit Function
    End If
    
    ' Can't have an index after the end of the array.
    If ReturnIndex > UpperBound Then
        GetOptionalArrayRangeReverse = ArgumentOutOfRange_UBound
        Exit Function
    End If
    
    ' Can't have a negative count.
    If ReturnCount < 0 Then
        GetOptionalArrayRangeReverse = ArgumentOutOfRange_NeedNonNegNum
        Exit Function
    End If
    
    ' Can't have the range extend past the beginning of the array.
    If ReturnIndex - ReturnCount + 1 < LowerBound Then
        GetOptionalArrayRangeReverse = Argument_InvalidCountOffset
    End If
End Function

''
' Verifies the index and count are within the bounds and size of a one-dimensional array.
'
' @param pSA A pointer to a SafeArray structure.
' @param Index The index into the array.
' @param Count The number of elements to include.
' @return If this function succeeds, then NO_ERROR is returned, otherwise
' and error exception code is returned.
'
Public Function VerifyArrayRange(ByVal pSafeArray As Long, ByVal Index As Long, ByVal Count As Long) As Long
    ' This function is optimized by not refactoring
    ' common sections with other helper rountine in
    ' order to cut down on total function calls.

    ' Check if the array is a null array.
    If pSafeArray = vbNullPtr Then
        VerifyArrayRange = ArgumentNull_Array
        Exit Function
    End If
    
    ' Ensure we only have a 1-Dimension array.
    If SafeArrayGetDim(pSafeArray) <> 1 Then
        VerifyArrayRange = Rank_MultiDimension
        Exit Function
    End If
    
    ' Can't have an index before the beginning of the array.
    If Index < SafeArrayGetLBound(pSafeArray, 1) Then
        VerifyArrayRange = ArgumentOutOfRange_LBound
        Exit Function
    End If
    
    ' Can't have a negative count.
    If Count < 0 Then
        VerifyArrayRange = ArgumentOutOfRange_NeedNonNegNum
        Exit Function
    End If
    
    ' Can't have the range extend past the end of the array.
    If Index + Count - 1 > SafeArrayGetUBound(pSafeArray, 1) Then
        VerifyArrayRange = Argument_InvalidCountOffset
    End If
End Function

''
' Verifies the index and count are within the bounds and size of a one-dimensional array.
' This version ensures that Index - Count does not extend below the lower bound.
'
' @param pSA A pointer to a SafeArray structure.
' @param Index The index into the array.
' @param Count The number of elements to include.
' @return If this function succeeds, then NO_ERROR is returned, otherwise
' and error exception code is returned.
'
Public Function VerifyArrayRangeReverse(ByVal pSafeArray As Long, ByVal Index As Long, ByVal Count As Long) As Long
    ' This function is optimized by not refactoring
    ' common sections with other helper rountine in
    ' order to cut down on total function calls.

    ' Check if the array is a null array.
    If pSafeArray = vbNullPtr Then
        VerifyArrayRangeReverse = ArgumentNull_Array
        Exit Function
    End If
    
    ' Ensure we only have a 1-Dimension array.
    If SafeArrayGetDim(pSafeArray) <> 1 Then
        VerifyArrayRangeReverse = Rank_MultiDimension
        Exit Function
    End If
    
    ' Can't have an index after the end of the array.
    If Index > SafeArrayGetUBound(pSafeArray, 1) Then
        VerifyArrayRangeReverse = ArgumentOutOfRange_UBound
        Exit Function
    End If
    
    ' Can't have a negative count.
    If Count < 0 Then
        VerifyArrayRangeReverse = ArgumentOutOfRange_NeedNonNegNum
        Exit Function
    End If
    
    ' Can't have the range extend past the beginning of the array.
    If Index - Count + 1 < SafeArrayGetLBound(pSafeArray, 1) Then
        VerifyArrayRangeReverse = Argument_InvalidCountOffset
    End If
End Function

''
' Throws specific exceptions based on an error code.
'
' @param ErrorCode The code that determines which exception to throw.
' @param ArrayName The name of the array in which the exception occurred.
' @param Index The index into the array at the time of the error.
' @param IndexName The name of the Index parameter to be included in the exception.
' @param Count The number of elements that were included in the verification of the range in the array.
' @param CountName The name of the Count parameter to be included in the exception.
' @param IsIndexMissing Used to help determine which parameter was missing in the original function call.
' @remarks This throws exceptions that are general cases about an Index and Count
' being valid within a given array. Not all exception types are represented here.
'
Public Sub ThrowArrayRangeException(ByVal ErrorCode As Long, ByRef ArrayName As String, ByVal Index As Long, ByRef IndexName As String, ByVal Count As Long, ByRef CountName As String, Optional ByVal IsIndexMissing As Boolean)
    Dim Message As String
    Message = Environment.GetResourceString(ErrorCode)
    Select Case ErrorCode
        Case ArgumentNull_Array:                Throw Cor.NewArgumentNullException(Message, ArrayName)
        Case Rank_MultiDimension:               Throw Cor.NewRankException(Message)
        Case Argument_ParamRequired:            Throw Cor.NewArgumentException(Message, IIf(IsIndexMissing, IndexName, CountName))
        Case ArgumentOutOfRange_LBound:         Throw Cor.NewArgumentOutOfRangeException(Message, IndexName, Index)
        Case ArgumentOutOfRange_UBound:         Throw Cor.NewArgumentOutOfRangeException(Message, IndexName, Index)
        Case ArgumentOutOfRange_NeedNonNegNum:  Throw Cor.NewArgumentOutOfRangeException(Message, CountName, Count)
        Case Argument_InvalidCountOffset:       Throw Cor.NewArgumentException(Message, CountName)
        Case Else:                              Throw Cor.NewArgumentException(Message)
    End Select
End Sub

''
' Verifies the Index and Count range remain inside the bounds of a 0-base list.
'
' @param ListSize The size of the list being checked against.
' @param Index The index into the list, starting at 0.
' @param Count The number of elements in the list to include in the verification.
' @return If success, then 0 is returned, otherwise an error code is returned.
'
Public Function VerifyListRange(ByVal RangeSize As Long, ByVal Index As Long, ByVal Count As Long) As Long
    If Index < 0 Then
        VerifyListRange = ArgumentOutOfRange_LBound ' this should be mapped to ArgumentOutOfRange_NeedNonNegNum
        Exit Function
    End If

    If Count < 0 Then
        VerifyListRange = ArgumentOutOfRange_NeedNonNegNum
        Exit Function
    End If

    If Index + Count > RangeSize Then
        VerifyListRange = Argument_InvalidCountOffset
    End If
End Function

''
' Throws a specific list exception based on the error code, providing the correct argument in the exception.
'
' @param ErrorCode The code of the exception to be thrown.
' @param ListName The name of the list or type of list that was used in verification.
' @param Index The index into the list.
' @param IndexName The name of the variable in the public function.
' @param Count The number of elements verified against the list.
' @param CountName The name of the count variable in the public function.
' @param IsIndexMissing Flag indicating if Index was a missing argument, otherwise Count was missing.
'
Public Sub ThrowListRangeException(ByVal ErrorCode As Long, ByVal Index As Long, ByRef IndexName As String, ByVal Count As Long, ByRef CountName As String, Optional ByVal IsIndexMissing As Boolean)
    Dim Message As String
    
    ' Two of the error codes are of the same type, but we need to distinguish
    ' between them. So we use ArgumentOutOfRange_LBound for one of the codes.
    ' Now get the message for the original error code instead.
    If ErrorCode = ArgumentOutOfRange_LBound Then
        Message = Environment.GetResourceString(ArgumentOutOfRange_NeedNonNegNum)
    Else
        Message = Environment.GetResourceString(ErrorCode)
    End If
    
    Select Case ErrorCode
        Case Argument_ParamRequired:                Throw Cor.NewArgumentException(Message, IIf(IsIndexMissing, IndexName, CountName))
        Case ArgumentOutOfRange_LBound:             Throw Cor.NewArgumentOutOfRangeException(Message, IndexName, Index)
        Case ArgumentOutOfRange_NeedNonNegNum:      Throw Cor.NewArgumentOutOfRangeException(Message, CountName, Count)
        Case Argument_InvalidCountOffset:           Throw Cor.NewArgumentException("The index plus the count extends past the end of the list or string", "Count")
    End Select
End Sub

''
' Assigns values to the missing Index and Count pair, returning any error codes.
'
' @param RangeSize The number of elements in the range to verify against.
' @param OptionalIndex The optional Index variable in the public interface.
' @param ReturnIndex The return value for Index.
' @param OptionalCount The optional Count variable in the public interface.
' @param ReturnCount The return value for Count.
' @return Returns NO_ERROR if successful, otherwise an error code is returned.
' @remarks This has a hardcoded lowerbound of 0 for the start of the list.
'
Public Function GetOptionalListRange(ByVal RangeSize As Long, _
                                     ByRef OptionalIndex As Variant, ByRef ReturnIndex As Long, _
                                     ByRef OptionalCount As Variant, ByRef ReturnCount As Long) As Long
    ' This function is optimized by not refactoring
    ' common sections with other helper rountine in
    ' order to cut down on total function calls.
    
    ' Get our optional values.
    Dim Result As Long
    Result = GetOptionalLongPair(OptionalIndex, 0, ReturnIndex, OptionalCount, RangeSize, ReturnCount)
    If Result <> NO_ERROR Then
        GetOptionalListRange = Result
        Exit Function
    End If
    
    ' Can't have an index before the start of the range.
    If ReturnIndex < 0 Then
        GetOptionalListRange = ArgumentOutOfRange_LBound    ' this should be mapped to ArgumentOutOfRange_NeedNonNegNum
        Exit Function
    End If
    
    ' Can't have a negative count.
    If ReturnCount < 0 Then
        GetOptionalListRange = ArgumentOutOfRange_NeedNonNegNum
        Exit Function
    End If
    
    ' Can't have the range extend past the beginning of the array.
    If ReturnIndex + ReturnCount > RangeSize Then
        GetOptionalListRange = Argument_InvalidCountOffset
    End If
End Function

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?