|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Conversion from 1.1 to 2.0 and WarningsI'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 Michael Jackson <michaeldjack***@cox.net> wrote:
> I've converted a large .NET 1.1 app to .NET 2.0. I allowed the IDE to to the What do you want it to return if an exception occurs before 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. 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. -- Jon Skeet - <sk***@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 "Michael Jackson" <michaeldjack***@cox.net> wrote: It's actually an error in C# to do what you're doing. If you only assign> 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? 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 |
|||||||||||||||||||||||