
Namespace Parameters

    Public MustInherit Class ParameterFormat
        Friend _errorCode As Long

        Public MustOverride Sub ParseParameter(ByRef parameterByteArray As Byte())

        Public MustOverride Sub ParseParameter(ByRef parameterByteArray As Byte(), _
                                               ByVal startIndex As Integer, _
                                               ByVal length As Integer)

        Friend Sub VerifyParameterLength(ByRef parameterByteArray As Byte(), _
                                         ByVal startIndex As Integer, _
                                         ByVal length As Integer)
            Dim verifyStep As Integer
            Dim errorMessage As String
            Dim exitLoop As Boolean

            verifyStep = 1
            errorMessage = ""
            exitLoop = False
            Do
                Select Case verifyStep
                    Case 1
                        If startIndex >= parameterByteArray.Length Then
                            errorMessage = "startIndex cannot exceed the parameterByteArray's length"
                        End If
                    Case 2
                        If length = 0 Then
                            errorMessage = "length cannot be 0"
                        End If
                    Case 3
                        If length > parameterByteArray.Length Then
                            errorMessage = "length cannot exceed parameterByteArray's length"
                        End If
                    Case 4
                        If startIndex + length > parameterByteArray.Length Then
                            errorMessage = "startIndex+length cannnot large than the parameterByteArray's length"
                        End If
                    Case Is >= 5
                        exitLoop = True
                    Case Else
                        'should not come here
                        Stop
                End Select


                If errorMessage <> "" Then
                    Throw New Exception(errorMessage)
                Else
                    verifyStep += 1
                End If

                If exitLoop = True Then
                    Exit Do
                End If
            Loop

        End Sub

        Public ReadOnly Property ErrorCode() As Long
            Get
                Return _errorCode
            End Get
        End Property

    End Class
End Namespace



