Conversion from 1.1 to 2.0 and Warnings

M

Michael Jackson

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
 
J

Jon Skeet [C# MVP]

Michael Jackson said:
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.

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.
 
B

Barry Kelly

Michael Jackson said:
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top