Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How to avoid "Variable is used before it has been assigned a value."

Avatar of KarlTheHopeless
KarlTheHopelessFlag for United Kingdom of Great Britain and Northern Ireland asked on
Visual Basic.NET
13 Comments1 Solution1751 ViewsLast Modified:
The code below leads to a Warning Message about variable i.

The message reads ... "Warning      Variable 'i' is used before it has been assigned a value. A null reference exception could result at runtime.

.......................

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\myvbastext\listoffilesandpaths.txt")
            MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            MyReader.Delimiters = New String() {","}
            Dim currentRow As String()
            While Not MyReader.EndOfData
                currentRow = MyReader.ReadFields()
                i += 1
                Nicknam(i) = currentRow(0)
                NameOfFile(i) = currentRow(1)
                PathOfFile(i) = currentRow(2)
                ExtensionOfFile(i) = currentRow(3)
                LengthOfRecords(i) = currentRow(4)
            End While
        End Using

....................

But what am I supposed to do to avoid this Warning?

If I use Dim before the Block - e.g. Dim i As Integer = 0 - then I get an Error Message telling me that  "Variable 'i' hides a variable in an enclosing block.

So, how should I re-write the code above to avoid all these things?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Shahan Ayyub
Shahan AyyubFlag of Pakistan imageStaff Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answers