Hey all, I have a little problem which I have solved but am looking for a touch of advice. The upgrade from '03 to '05 has be smooth enough, but there is a little change which seems stupid to me - and therefore Im wondering if im doing something fundamentally wrong.
A MS-SQL DB method is as follows:
I end up with a warning on the last line of the Try..Catch telling me 'cn' may not have been initialised. Fair enough, but as you can see if I explicitly set 'cmd' to Nothing when it's created the warning on that line disappears.
So what exactly is the correct approach here? Is a variable not null (Nothing) before it is assigned?!? This seems to go against all my experience of C and Java..![Confused :confused: :confused:](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
Thanks all
A MS-SQL DB method is as follows:
VB.NET:
Public Function Execute(ByVal sql As String) As Int32
Dim cn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand = Nothing
Dim ret As Int32
Try
cn = New SqlClient.SqlConnection(Me.ConnectionString)
cn.Open()
cmd = New SqlClient.SqlCommand()
cmd.Connection = cn
cmd.CommandText = sql
ret = cmd.ExecuteNonQuery()
Return ret
Catch ex As Exception
msgbox(ex.Message)
Return Nothing
Finally
If Not cmd Is Nothing Then cmd.Dispose()
If Not cn Is Nothing Then cn.Dispose()
End Try
End Function
So what exactly is the correct approach here? Is a variable not null (Nothing) before it is assigned?!? This seems to go against all my experience of C and Java..
Thanks all
Last edited: