Coding correct/incorrect?

Discussion in 'ASP .Net' started by Rob Meade, Dec 27, 2006.

  1. Rob Meade

    Rob Meade Guest

    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
     
    Rob Meade, Dec 27, 2006
    #1
    1. Advertising

  2. Rob Meade

    Hans Kesting Guest

    > 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
     
    Hans Kesting, Dec 28, 2006
    #2
    1. Advertising

  3. Rob Meade

    Rob Meade Guest

    "Hans Kesting" wrote ...

    > 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
     
    Rob Meade, Dec 28, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. calmar
    Replies:
    11
    Views:
    896
    calmar
    Feb 21, 2006
  2. Sullivan WxPyQtKinter
    Replies:
    1
    Views:
    411
    Tim Roberts
    Apr 7, 2006
  3. s.subbarayan

    Is this correct according to C coding standards?

    s.subbarayan, Mar 8, 2005, in forum: C Programming
    Replies:
    8
    Views:
    544
    Keith Thompson
    Mar 10, 2005
  4. Sky
    Replies:
    1
    Views:
    141
  5. Graham Mattingley
    Replies:
    1
    Views:
    380
    Martin Honnen
    Nov 27, 2003
Loading...

Share This Page