Do you think this is a legitimate use of GoTo in vb.net? Does anyone have a better solution for this problem that creates a generic ExecuteNonQuery?
'==========================================================================
Public Shared Function ReturnParameterNonQuery(ByVal sSql As String, _
Optional ByVal p1 As String = "-9999", _
Optional ByVal p2 As String = "-9999", _
Optional ByVal p3 As String = "-9999", _
'… continue for as many parameters as you would have maximum in your project.
'of course this wouldn’t work if I had a legitimate value of “-9999”
Optional ByVal p25 As String = "-9999", _
Optional ByVal p26 As String = "-9999") As Int16
'Test if these parameters have values.
Dim conn As New DataAccess 'Connect to database
Dim cmd As New OracleCommand(sSql, conn.getconn)
If p1 = "-9999" Then
GoTo NoMoreParameters
Else
cmd.Parameters.AddWithValue("p1", p1)
End If
If p2 = "-9999" Then
GoTo NoMoreParameters
Else
cmd.Parameters.AddWithValue("p2", p2)
End If
… for as many parameters as you would have maximum in your project.
If p26 = "-9999" Then
GoTo NoMoreParameters
Else
cmd.Parameters.AddWithValue("p26", p26)
End If
'Don’t you think this is a legitimate use for a GoTo? Saves time!
NoMoreParameters:
ReturnParameterNonQuery = cmd.ExecuteNonQuery()
cmd.Dispose()
End Function
kfoxPosted Jun 11, 2007, 12:14 PM
AlanPosted Jun 8, 2007, 11:25 AM
TBH, I think you'd be better putting the optional string parameters into an array which would cut down the code considerably as well as dispensing with the use of 'goto'. Something like this:
Dim params As String() = New String(){p1, p2, p3, ....., p25, p26}
Dim conn As New DataAccess 'Connect to database
Dim cmd As New OracleCommand(sSql, conn.getconn)
For i As Integer = 0 To params.Length -1
If params(i) = "-9999" Then
ReturnParameterNonQuery = cmd.ExecuteNonQuery()
cmd.Dispose()
Exit For
Else
cmd.Parameters.AddWithValue("p" & (i + 1).ToString(), params(i))
End If
Next