In fact, this VB Code is so good, that it's bad (which is how it ended up here, afterall). You see, James' colleague understood that hard coding things is bad. He even resisted coding "0", even though it is only used once, and even though it is used a few lines below the constant ... now that's good.

Public Function CountDuplicates(byval dataConnection as String) as integer 
     Dim lCount As Integer
     Dim connectionString As String
     connectionString = dataConnection

     Dim storedProcedureName As String
     storedProcedureName = msUP_DUPLICATE_COUNT


     'Stored Procedure Parameter Indices
     Const SP_PARAM_INDEX_IN_MESSAGE_ID As Integer = 0
     Const SP_PARAM_INDEX_IN_USER_ID As Integer = 1
     Const SP_PARAM_INDEX_IN_DOC_TYPE As Integer = 2




     'Stored Procedure Parameter Indices
     Const SP_PARAM_NAME_IN_MESSAGE_ID As String = "@appIdentifer"
     Const SP_PARAM_NAME_IN_USER_ID As String = "@userId"
     Const SP_PARAM_NAME_IN_DOC_TYPE As String = "@docType"

     Const SP_PARAM_TYPE_IN_MESSAGE_ID As SqlDbType = SqlDbType.NVarChar
     Const SP_PARAM_TYPE_IN_USER_ID As SqlDbType = SqlDbType.Int
     Const SP_PARAM_TYPE_IN_DOC_TYPE As SqlDbType = SqlDbType.VarChar


     'Populate array of Stored Procedure parameters
     Dim commandParameters() As SqlParameter
     ReDim commandParameters(SP_PARAM_INDEX_IN_DOC_TYPE)

     commandParameters(SP_PARAM_INDEX_IN_MESSAGE_ID) = New SqlParameter(SP_PARAM_NAME_IN_MESSAGE_ID, appId)
     commandParameters(SP_PARAM_INDEX_IN_USER_ID) = New SqlParameter(SP_PARAM_NAME_IN_USER_ID, userId)
     commandParameters(SP_PARAM_INDEX_IN_DOC_TYPE) = New SqlParameter(SP_PARAM_NAME_IN_DOC_TYPE, docType)

     commandParameters(SP_PARAM_INDEX_IN_MESSAGE_ID).SqlDbType = SP_PARAM_TYPE_IN_MESSAGE_ID
     commandParameters(SP_PARAM_INDEX_IN_USER_ID).SqlDbType = SP_PARAM_TYPE_IN_USER_ID
     commandParameters(SP_PARAM_INDEX_IN_DOC_TYPE).SqlDbType = SP_PARAM_TYPE_IN_DOC_TYPE


     'Make the database query.
     Dim dbQueryReturn As Object
     dbQueryReturn = DAB.ExecuteScalar( _
           connectionString _
           , CommandType.StoredProcedure _
           , storedProcedureName _
           , commandParameters _
           )

     lCount = CType(dbQueryReturn, Integer)
     Return lCount
End Function

 

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!