R
Rob Meade
Hi there,
I have my own "way" I guess of coding, like we all do, whilst I try to
maintain consistency throughout my own applications I often wonder whether
there are better ways of doing things...since I started using Visual Studio
2005 I've been quite pleased with the messages it gives you when you make
mistakes, and its underlining of unused variables and so on...
One thing that, not so much puzzles me, but makes me think over and over
again is the "unused variables"....here's an example..
' exception handling
Try
' instantiate
sqlCommand = GetSQLCommand()
' set command properties
sqlCommand.CommandText = commandText
sqlCommand.CommandType = commandType
sqlCommand.Connection = sqlConnection
' iterate
For iteration = 0 To (parameters.Count - 1)
' add parameter
sqlCommand.Parameters.Add(parameters(iteration))
Next
Catch ex As Exception
' housekeeping
Dispose(sqlCommand)
Dispose(sqlConnection)
' TODO: Unable to set command properties
End Try
In the above example, Visual Studio unlines the "sqlCommand" in the
housekeeping section - Dispose(sqlCommand) - because it believes that it
could well be "null" and might throw and exception (null reference
exception)...
Now, I know it wont(shouldn't!), because in order to get into the "Catch"
part of the exception handling it would have to have begun in the "Try"
section, and in there I create a new instance of the object. I guess there
could be something, not that I know what, but something that might prevent
the object from being created, and therefore I can see why it does
this...however, in my Dispose function I also have code which checks to see
if the object exists before trying to do anything else with it...
Please note that this is just one example out of many in my project where I
use exception handling, but the query is the same with all - what I'm
wondering is whether there's a way that I can "satisfy" Visual Studio so
that it ignores this "problem" etc. The only way I can see is to create the
object outside of the "Try", but that kinda defeats the point....
Any info on this would be appreciated, I'm sure we all have our different
ways of doing things and I'd like to see if anyone else does things similar
or completely different to me. I also appreciate that these are only
"warnings" and wont necessarily cause me a load of grief if I have taken
them into account - just a case of a developer trying to be tidy I guess...
Cheers
Rob
I have my own "way" I guess of coding, like we all do, whilst I try to
maintain consistency throughout my own applications I often wonder whether
there are better ways of doing things...since I started using Visual Studio
2005 I've been quite pleased with the messages it gives you when you make
mistakes, and its underlining of unused variables and so on...
One thing that, not so much puzzles me, but makes me think over and over
again is the "unused variables"....here's an example..
' exception handling
Try
' instantiate
sqlCommand = GetSQLCommand()
' set command properties
sqlCommand.CommandText = commandText
sqlCommand.CommandType = commandType
sqlCommand.Connection = sqlConnection
' iterate
For iteration = 0 To (parameters.Count - 1)
' add parameter
sqlCommand.Parameters.Add(parameters(iteration))
Next
Catch ex As Exception
' housekeeping
Dispose(sqlCommand)
Dispose(sqlConnection)
' TODO: Unable to set command properties
End Try
In the above example, Visual Studio unlines the "sqlCommand" in the
housekeeping section - Dispose(sqlCommand) - because it believes that it
could well be "null" and might throw and exception (null reference
exception)...
Now, I know it wont(shouldn't!), because in order to get into the "Catch"
part of the exception handling it would have to have begun in the "Try"
section, and in there I create a new instance of the object. I guess there
could be something, not that I know what, but something that might prevent
the object from being created, and therefore I can see why it does
this...however, in my Dispose function I also have code which checks to see
if the object exists before trying to do anything else with it...
Please note that this is just one example out of many in my project where I
use exception handling, but the query is the same with all - what I'm
wondering is whether there's a way that I can "satisfy" Visual Studio so
that it ignores this "problem" etc. The only way I can see is to create the
object outside of the "Try", but that kinda defeats the point....
Any info on this would be appreciated, I'm sure we all have our different
ways of doing things and I'd like to see if anyone else does things similar
or completely different to me. I also appreciate that these are only
"warnings" and wont necessarily cause me a load of grief if I have taken
them into account - just a case of a developer trying to be tidy I guess...
Cheers
Rob