Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Conversion from 1.1 to 2.0 and Warnings

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Jackson - 12 Jul 2006 23:56 GMT
I've converted a large .NET 1.1 app to .NET 2.0. I allowed the IDE to to the
conversion, and had basically no problems at all. However, the VB.NET
compiler give me several hundred Warnings,  and most refer to a variable
being used before it is assigned a value. I have tons of subs and functions
where I Dim a variable at the top of the sub or function, outside any Try or
If blocks, (e.g Dim Result As String), then assign to that variable within
the Try or If block, and possibly return the variable or Dispose of it or
set it to nothing.

Is my coding technique wrong? How should I code this? Should I assign
String.Empty to string variables?

Thanks for any input.

Michael
Jon Skeet [C# MVP] - 13 Jul 2006 00:05 GMT
> I've converted a large .NET 1.1 app to .NET 2.0. I allowed the IDE to to the
> conversion, and had basically no problems at all. However, the VB.NET
[quoted text clipped - 4 lines]
> the Try or If block, and possibly return the variable or Dispose of it or
> set it to nothing.

What do you want it to return if an exception occurs before the
assignment then?

> Is my coding technique wrong?

Yes, IMO. You've left yourself open for problems.

> How should I code this?

Always assign a value to a variable before it might be used.

> Should I assign String.Empty to string variables?

The answer to last entirely depends on the answer to the first question
I asked. If you want an empty string to be returned if you've caught an
exception before a "real" value was assigned, then yes. If you want to
return Nothing, set the value to Nothing to start with, etc.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Barry Kelly - 13 Jul 2006 00:08 GMT
> I've converted a large .NET 1.1 app to .NET 2.0. I allowed the IDE to to the
> conversion, and had basically no problems at all. However, the VB.NET
[quoted text clipped - 7 lines]
> Is my coding technique wrong? How should I code this? Should I assign
> String.Empty to string variables?

It's actually an error in C# to do what you're doing. If you only assign
to a variable in one 'half' of an if statement and then use the variable
after the if statement, it's possible that you've made a mistake and
used a variable that you haven't given a value to. In C++, the variable
would have an undefined (typically garbage) value, and usually results
in a warning these days too.

String variables will probably have a nil value by default, presuming
the VB.NET compiler is producing the locals with the 'init' attribute
set - I haven't checked, but that's probably what happens. Ideally,
you'll pre-set values for all variables which aren't always set in both
branches of an if statement, or for those which are only assigned to
inside a try block but are used in the corresponding finally or except
blocks.

-- Barry

Signature

http://barrkel.blogspot.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.