0

I don't have a lot of experience working with visual basic so please be gentle. I suppose to make work a program that was programmed by someone else (for many reasons that person won't keep working on it), so everything was fine until I reached this piece of code:

Function ExisteProceso02(ByVal CodigoBarra As String) As Boolean
    Dim miComando As New SqlCommand
    Dim miDataAdapter As New SqlDataAdapter
    Dim Table As New DataTable
    Dim strSQL As String

    strSQL = "Select * From PCT_GuiasDespacho Where CodigoBarra = '" & CodigoBarra & "' And 
    PreAnalisis = 'SI'"
    miComando.CommandText = strSQL
    miComando.Connection = miConexion
    miDataAdapter.SelectCommand = miComando
    miDataAdapter.Fill(Table)
    If Table.Rows.Count = 0 Then
        ExisteProceso02 = False
    Else
        ExisteProceso02 = True
    End If
    miComando.Dispose()
    miDataAdapter.Dispose()
    Table.Dispose()
End Function

My problem is that this function is assigning itself a value and it isn't called anywhere else in the code (I use the go to definition.) but it seems to be doing something and I don't know what is it called this property so I can reach information about it.

Can someone please tell me how this works? Or at least what is it?

Thank you.

3
  • 1
    ExisteProceso02 = False basically means "set the return value of ExisteProceso02 to false". Don't worry: it isn't overwriting the function's definition or anything like that.
    – Dai
    Jan 13, 2021 at 12:59
  • 2
    Also, you really should rewrite that function to use SqlParameter as it's vulnerable to SQL injection (i.e. it will break and reveal secret information if CodigoBarra contains an apostrophe).
    – Dai
    Jan 13, 2021 at 13:00
  • 1
    This person probably was a VB6 dev. You can use return If (Table.Rows.Count = 0, False, True). You just need SqlCommand and ExcuteScalar() (if you actually need a method for this at all). The SqlCommand (and all the other objects - DataTable and DataAdapter - if you want to keep them) should be declared with Using statements, to dispose of them implicitly (no need to call Dispose() explicitly). Or use a Try/Finally block and dispose of disposable object in the Finally block, which is called in any case, even if exception are raised in the meanwhile.
    – Jimi
    Jan 13, 2021 at 14:33

3 Answers 3

1

This is a somewhat older (but still often useful) syntax for returning a value in Visual Basic.

Whereas this will end execution of the function:

Return False

This will instead set the return value but continue executing the function:

ExisteProceso02 = False

Which among other things allows for updating to a new return value later if logically necessary. (Or in this case allows for explicitly disposing of some objects before returning. Though there are better ways to do that now.)

So basically, in Visual Basic, setting a value to the name of the function within that function means "when the function ends, return this value".

1

Declare you database objects in the method where they are used so they can be closed and disposed. Apparently, you have declared the connection elsewhere. Using...End Using blocks will handle this for you even if there is an error. Always use parameters to avoid Sql injection and other reasons. I had to guess at the datatype of the parameter. Check your database for the correct type.

As to the assignment of the return value to the name of the function, that is old vb6 syntax. It will still work. The current syntax is to use the Return keyword which brings this in line with other languages.

Since all you need is the count, I adjusted the Sql command to ask for just that. Then you can use .ExecuteScalar. Sql Server also provides If Exists which might be a bit faster for a large table because it stops looking as soon as it finds a match instead of scanning the entire table.

Private ConStr As String = "Your connection string"

Function ExisteProceso02(ByVal CodigoBarra As String) As Boolean
    Dim ReturnCount As Integer
    Dim strSQL = "Select Count(*) 
                   From PCT_GuiasDespacho 
                   Where CodigoBarra = @CodigoBarra 
                   And PreAnalisis = 'SI';"
    Using miConexion As New SqlConnection(ConStr),
            miComando As New SqlCommand(strSQL, miConexion)
        miComando.Parameters.Add("@CodigoBarra", SqlDbType.NVarChar).Value = CodigoBarra
        miConexion.Open()
        ReturnCount = CInt(miComando.ExecuteScalar)
    End Using
    If ReturnCount = 0 Then
        Return False
    Else
        Return True
    End If
End Function
1
  • Upvoted for best-practices! Though I think the code should use NVarChar instead of VarChar - especially given they're clearly a nonanglophone so it's very likely that accented and other non-VarChar-safe characters will be used.
    – Dai
    Jan 15, 2021 at 13:34
-1

A slight rewrite

Function ExisteProceso02(ByVal CodigoBarra As String) As Boolean
    Dim rv As Boolean 'return value <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Dim miComando As New SqlCommand
    Dim miDataAdapter As New SqlDataAdapter
    Dim Table As New DataTable
    Dim strSQL As String
    'the following statement should be rewritten 
    strSQL = "Select * From PCT_GuiasDespacho Where CodigoBarra = '" & CodigoBarra & "' And PreAnalisis = 'SI'"
    miComando.CommandText = strSQL
    miComando.Connection = miConexion
    miDataAdapter.SelectCommand = miComando
    miDataAdapter.Fill(Table)
    ' <<<<<<<<<<<<<<<<<<<<<<<<<
    rv = Table.Rows.Count <> 0
    ' <<<<<<<<<<<<<<<<<<<<<<<<<
    miComando.Dispose()
    miDataAdapter.Dispose()
    Table.Dispose()
    Return rv ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
End Function
2
  • I downvoted this answer because it's still vulnerable to SQL injection and doesn't safely dispose of all IDisposable objects if an exception were to be thrown (we should not be encouraging bad habits!) - and using SqlDataAdapter is excessive - just use a SqlDataReader.
    – Dai
    Jan 15, 2021 at 13:32
  • @Dai hence the comment
    – dbasnett
    Jan 26, 2021 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.