473,569 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1060
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******** *************@i 3g2000cwc.googl egroups.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******** *************@i 3g2000cwc.googl egroups.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******** *************@i 3g2000cwc.googl egroups.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******@onlin e.excite.comwro te in message
news:O7******** ******@TK2MSFTN GP06.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******** *************@i 3g2000cwc.googl egroups.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******@onlin e.excite.comwro te in message
news:O7******** ******@TK2MSFTN GP06.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******** *************@i 3g2000cwc.googl egroups.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******** *************@i 3g2000cwc.googl egroups.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
2104
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 automatically builds querystrings for my redirect URLS. The code for this follows. It defines an ASP Dictionary object, and key/value pairs for each,...
242
13228
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 comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on...
54
2890
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 post the code? Alternatively, maybe someone would be willing to put it on the web for some days? I'm reluctant to sign up with a freespace provider...
12
3278
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. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would...
10
1645
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. However, class CVehicle is an abstract base class, since every object of it is member of subclass CCar or subclass CBicycle.
26
2866
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: error: using obsolete binding at 'j'
232
13119
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 set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an...
13
2302
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 add-ons for this purpose? If not, any urls which demonstrates code snippets management?
9
1896
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 user sees all the colors listed on the page with hyperlinks that open the "mixes" page. The mixes page goes through each record, compares it with...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7931
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7682
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6290
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3663
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
954
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.