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 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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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....
|
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...
|
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.
...
|
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...
|
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:
|
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
...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |