Daniel Wilson
asked on
Warning: Variable is used before it has been assigned a value -- best practice?
Warning 3 Variable 'cmd' is used before it has been assigned a value. A null reference exception could result at runtime.
The line generating this error is in my finally block. See the snippet below:
It's the IsNothing check generating the warning.
Now that STUPID. I'm checking for the Nothing condition.
The way I see to suppress the warning message is
Dim cmd as sqlCommand = Nothing
If I say that it is nothing, it doesn't warn me that it COULD be nothing.
Now, these warnings are here for a purpose -- so that if I DO forget to assign a value, the compiler warns me.
How do I suppress spurious warnings without suppressing the warnings I should pay attention to?
Thanks!
The line generating this error is in my finally block. See the snippet below:
It's the IsNothing check generating the warning.
Now that STUPID. I'm checking for the Nothing condition.
The way I see to suppress the warning message is
Dim cmd as sqlCommand = Nothing
If I say that it is nothing, it doesn't warn me that it COULD be nothing.
Now, these warnings are here for a purpose -- so that if I DO forget to assign a value, the compiler warns me.
How do I suppress spurious warnings without suppressing the warnings I should pay attention to?
Thanks!
Dim cmd As SqlCommand
Try
'Do great things w/ cmd
Catch e As Exception
Throw e
Finally
If Not IsNothing(cmd) AndAlso Not IsNothing(cmd.Connection) AndAlso cmd.Connection.State = ConnectionState.Open Then
cmd.Connection.Close()
End If
End Try
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I don't use vb.net alot, but rather c# :)
Dim cmd As SqlCommand = new SqlCommand
is matching what I use in c#...
ASKER
Well, thanks for the ideas.
I'm working in a VB shop, so my C# is weak :)
I'm working in a VB shop, so my C# is weak :)
Hi DanielWilson;
You can turn off that warning by going to the projects property page, select Compile tab and change the "Use of variable prior to assignment" to None.
Fernando
SP32-38.jpg
You can turn off that warning by going to the projects property page, select Compile tab and change the "Use of variable prior to assignment" to None.
Fernando
SP32-38.jpg
ASKER
Ok ... but it IS a good warning if I'm NOT checking for whether it's Nothing.
Is there a way to write the code so that I get the warning if I have been careless?
Is there a way to write the code so that I get the warning if I have been careless?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I got it:
replace
Dim cmd As SqlCommand
with
Dim cmd As New SqlCommand
the keyword "New" shall be used to generate a new object...so that when so say
cmd.Connection.Close()
there is a real object to close down.
Cheers
replace
Dim cmd As SqlCommand
with
Dim cmd As New SqlCommand
the keyword "New" shall be used to generate a new object...so that when so say
cmd.Connection.Close()
there is a real object to close down.
Cheers
Yes kouroshparsa, already stated on post http://#a22164803
Try to initialise the value to Nothing:
Dim cmd As SqlCommand = Nothing
Dim cmd As SqlCommand = Nothing
Silly me, didn't read the thread properly.
But still, my personal preference is to assign to Nothing when declaring variable, then watch for places where this variable could possibly generate Null exception (or whatever exception).
Potential places for such condition are not many, most probably in catch or finally blocks.
But still, my personal preference is to assign to Nothing when declaring variable, then watch for places where this variable could possibly generate Null exception (or whatever exception).
Potential places for such condition are not many, most probably in catch or finally blocks.
ASKER
Thanks, Guys!
ASKER
I'm still passing a (possibly null) reference to the IsNothing function.