473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Coding correct/incorrect?

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
Dec 27 '06 #1
2 1219
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
Dec 28 '06 #2
"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
Dec 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

136
by: Merrill & Michele | last post by:
A derangement is a mapping of a set onto itself leaving no element fixed. I realized that that was what I was programming when I was asked to randomly determine who buys presents for whom this...
17
by: Domestos | last post by:
Hi all, I was wondering if there is a standard when mixing PHP and HTML script on a page... and if there were any articles on this subject... i.e. do I make every page a .php page on my...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
19
by: auratius | last post by:
http://www.auratius.co.za/CSharpCodingStandards.html Complete CSharp Coding Standards 1. Naming Conventions and Styles 2. Coding Practices 3. Project Settings and Project Structure 4....
6
by: =?Utf-8?B?ZGljazIz?= | last post by:
I am just a beginner coding in VB6. I am trying to write a simple code to add 2 numbers together and put the answer in a text box I have one textbox open to accept each number, a textbox to accept...
3
lee123
by: lee123 | last post by:
I have a problem getting the correct to count +1 every time I get an answer right and the incorrect is the same. I have two lbl's named number1 and number2 which produces a Rnd# in each lbl. ...
0
by: readnlearn | last post by:
hai, i have written this below code for displaying captcha image whenever i entered incorrect uname,password in login page. for that i disable the controls of captcha like textbox,labels,button and...
3
by: alexdavies82 | last post by:
I am having a problem producing a relatively simple PHP code. Whenever I run it it gives the following fault message:
27
by: dennis1989 | last post by:
Im having problem running the program. Its say i have incorrect syntax at my strsql coding Private Sub ComboBox1_Change() Dim rst As ADODB.Recordset 'Initialize Recordset ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.