473,405 Members | 2,334 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,405 software developers and data experts.

Code Standard 2.0 new objects outside of Try Block

I have question about code standards in VS 2005

I have been declaring objects before a try block in case I would ever
want to refer to them in 'Catch' block. When I converted my apps to 2.0
I get warnings that objects have been declared, but not initialized.
I've hesitated to actually 'New' an object outside of a try in the
event of an exception occurring. For example, if I new up a Connection
object with a connection string with an incorrect syntax it will throw
an InvalidArgument exception.

What's a best practice for this?

(I don't use Try Blocks in every method, but when I do, I want to make
sure that things are being trapped)

Thanks,

Gabe

Aug 9 '06 #1
6 1050
Gabe,

Just assign null to the variables - this should stop those warnings. You
don't necessarily have to instantiate the objects.

"gabe" <ga*****@gmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
I have question about code standards in VS 2005

I have been declaring objects before a try block in case I would ever
want to refer to them in 'Catch' block. When I converted my apps to 2.0
I get warnings that objects have been declared, but not initialized.
I've hesitated to actually 'New' an object outside of a try in the
event of an exception occurring. For example, if I new up a Connection
object with a connection string with an incorrect syntax it will throw
an InvalidArgument exception.

What's a best practice for this?

(I don't use Try Blocks in every method, but when I do, I want to make
sure that things are being trapped)

Thanks,

Gabe

Aug 9 '06 #2
I believe you'll want to create these objects within the Try/Catch block so that
they are available within the block and then immediately disposed when the block
is exited.

Warnings are just that, they don't necessarily mean the code is going to crash.

"gabe" <ga*****@gmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
>I have question about code standards in VS 2005

I have been declaring objects before a try block in case I would ever
want to refer to them in 'Catch' block. When I converted my apps to 2.0
I get warnings that objects have been declared, but not initialized.
I've hesitated to actually 'New' an object outside of a try in the
event of an exception occurring. For example, if I new up a Connection
object with a connection string with an incorrect syntax it will throw
an InvalidArgument exception.

What's a best practice for this?

(I don't use Try Blocks in every method, but when I do, I want to make
sure that things are being trapped)

Thanks,

Gabe

Aug 9 '06 #3
Gabe

Are you sure that the variables are real usable or/and used.
Setting them to null is just a waste of processing time in my opinion.
(I assume you are using VB.Net, were we (regulars from VBNet language
newsgroup) hope that this warnings will be more selective in future as it is
now very much based on C# coding)

Cor

"gabe" <ga*****@gmail.comschreef in bericht
news:11*********************@i3g2000cwc.googlegrou ps.com...
>I have question about code standards in VS 2005

I have been declaring objects before a try block in case I would ever
want to refer to them in 'Catch' block. When I converted my apps to 2.0
I get warnings that objects have been declared, but not initialized.
I've hesitated to actually 'New' an object outside of a try in the
event of an exception occurring. For example, if I new up a Connection
object with a connection string with an incorrect syntax it will throw
an InvalidArgument exception.

What's a best practice for this?

(I don't use Try Blocks in every method, but when I do, I want to make
sure that things are being trapped)

Thanks,

Gabe

Aug 9 '06 #4
I do this too - set the variables to null.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Accept the Unexpected.

"Siva M" <sh******@online.excite.comwrote in message
news:O7**************@TK2MSFTNGP06.phx.gbl...
Gabe,

Just assign null to the variables - this should stop those warnings. You
don't necessarily have to instantiate the objects.

"gabe" <ga*****@gmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
I have question about code standards in VS 2005

I have been declaring objects before a try block in case I would ever
want to refer to them in 'Catch' block. When I converted my apps to 2.0
I get warnings that objects have been declared, but not initialized.
I've hesitated to actually 'New' an object outside of a try in the
event of an exception occurring. For example, if I new up a Connection
object with a connection string with an incorrect syntax it will throw
an InvalidArgument exception.

What's a best practice for this?

(I don't use Try Blocks in every method, but when I do, I want to make
sure that things are being trapped)

Thanks,

Gabe

Aug 9 '06 #5
Thanks --- I'll follow that advice.

I was thinking in 1.1 that only class member variables were't
initialized. I'll run it through the debugger to verify, but I'll Null
them anyway to satisfy the compiler.

Kevin Spencer wrote:
I do this too - set the variables to null.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Accept the Unexpected.

"Siva M" <sh******@online.excite.comwrote in message
news:O7**************@TK2MSFTNGP06.phx.gbl...
Gabe,

Just assign null to the variables - this should stop those warnings. You
don't necessarily have to instantiate the objects.

"gabe" <ga*****@gmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
I have question about code standards in VS 2005

I have been declaring objects before a try block in case I would ever
want to refer to them in 'Catch' block. When I converted my apps to 2.0
I get warnings that objects have been declared, but not initialized.
I've hesitated to actually 'New' an object outside of a try in the
event of an exception occurring. For example, if I new up a Connection
object with a connection string with an incorrect syntax it will throw
an InvalidArgument exception.

What's a best practice for this?

(I don't use Try Blocks in every method, but when I do, I want to make
sure that things are being trapped)

Thanks,

Gabe
Aug 9 '06 #6
My take on it would be something like:

private sub xyz

dim a as object
try
a = new (object)
a.blahblah...

catch
end try
end sub

it keeps all the variable declarations together at a place, however you
instantiate the object only when it is required.
Flipside from this is of course that the object lifeline gets extended a wee
bit longer than it would be if you were to declare it within a try catch
block inside an if construct or some loop.
But then it would not be too advisable to have really long methods anyway.
The object does get collected once the method is executed.

HTH
Nick
"Cor Ligthert [MVP]" wrote:
Gabe

Are you sure that the variables are real usable or/and used.
Setting them to null is just a waste of processing time in my opinion.
(I assume you are using VB.Net, were we (regulars from VBNet language
newsgroup) hope that this warnings will be more selective in future as it is
now very much based on C# coding)

Cor

"gabe" <ga*****@gmail.comschreef in bericht
news:11*********************@i3g2000cwc.googlegrou ps.com...
I have question about code standards in VS 2005

I have been declaring objects before a try block in case I would ever
want to refer to them in 'Catch' block. When I converted my apps to 2.0
I get warnings that objects have been declared, but not initialized.
I've hesitated to actually 'New' an object outside of a try in the
event of an exception occurring. For example, if I new up a Connection
object with a connection string with an incorrect syntax it will throw
an InvalidArgument exception.

What's a best practice for this?

(I don't use Try Blocks in every method, but when I do, I want to make
sure that things are being trapped)

Thanks,

Gabe


Aug 10 '06 #7

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

Similar topics

1
by: Ken Fine | last post by:
I have a menu system that has nodes that can be opened or closed. In an effort to make my code more manageable, I programmed a little widget tonight that keeps track of the open/active item and...
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
54
by: Martin Eisenberg | last post by:
Hi, I've written a program that I'd like to hear some opinions on regarding my C++ usage. As a program it's smallish, but on Usenet 300 lines seem a bit much. Do you think it's appropriate to...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
10
by: jeroendeurinck | last post by:
Hi all, I'm a newbe, so sorry if this question would be inappropriate here. Nevertheless I try. --- Suppose I have a class CTraffic in which several objects of class CVehicle move around....
26
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37:...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
9
by: TF | last post by:
Hello all, I made a ASP.NET 2.0 site that shows possible "recipes" for paint colors stored in an access dbase. Basically, 1000 colors are stored with specific RGB values in separate columns. A...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.