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

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.

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

Use a different varaiable in the using block:
I think you have the  code like this:

Dim i as integer

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

So you can do like this:

Dim i as  integer

 
Using MyReader As  New  Microsoft.VisualBasic.FileIO.TextFieldParser("c:\myvbastext\listoffilesandpaths.txt")
           Dim j as integer = 0
             MyReader.TextFieldType =  Microsoft.VisualBasic.FileIO.FieldType.Delimited
             MyReader.Delimiters = New String() {","}
           Dim  currentRow  As String()
           While Not MyReader.EndOfData
                 currentRow = MyReader.ReadFields()
               j += 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
...................
.................  'i' is accessible here as well so 'j' is a seperate variable you are using within 'using' block.
 ....................
....................
....................

 Since variable 'i'  is using inside the 'using' block and outside the  'using' block as well  so it is hiding the value within the 'using'  block.

Random Solutions  
 
programming4us programming4us