Coding correct/incorrect?

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
 
H

Hans Kesting

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

Theoretically, your GetSQLCommand() could have thrown the exception. In
that case the assignment to sqlCommand did not happen when control
reached the Catch, so it will be null there ...

Hans Kesting
 
R

Rob Meade

...
Theoretically, your GetSQLCommand() could have thrown the exception. In
that case the assignment to sqlCommand did not happen when control reached
the Catch, so it will be null there ...

That also has its own exception handling though and would have been dealt
with within that function.

I guess what I was after was a better way to get around this problem - any
ideas?

Regards

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top