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

KarlTheHopeless
KarlTheHopeless used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shahan AyyubSenior Software Engineer

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

You can use this line  for declaration of variable:
Dim  i As Integer = 0    
 
>>>>> I get an Error  Message telling me that  "Variable 'i' hides a variable in an enclosing  block.

See this:
http://msdn.microsoft.com/en-us/library/wtk40des%28VS.80%29.aspx

Author

Commented:
See what?

LOL!
Shahan AyyubSenior Software Engineer

Commented:
LOL! is for what ?

Author

Commented:
What are you asking me to 'see'?
Shahan AyyubSenior Software Engineer

Commented:
See this link: ( you need to use a new variable within the block)
http://msdn.microsoft.com/en-us/library/wtk40des%28VS.80%29.aspx
                               

Is this link was not visible in the last post ??

Author

Commented:
No. There was no link visible.

I'm off to look at your link.

Author

Commented:
Don't understand what to do.

How can I change the above code so that it works?
Shahan AyyubSenior Software Engineer

Commented:
Okay write this on Google:

Variable '<variablename>' hides a variable in an enclosing block          

you will see a first link of msdn.microsoft.com/

Author

Commented:
I've tried Google. I do not understand what to do.

Can you please just tell me what to do with the code above
Senior Software Engineer
Commented:
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.
Shahan AyyubSenior Software Engineer

Commented:
text from the link:

>>>>>>>>>>>>>Variable  '<variablename>' hides a variable in an enclosing block          

A  variable enclosed in a block has the same name as another local  variable.

Error ID: BC30616To correct this error

Rename the variable in the enclosed block  so that it is not the same as any other local
variables.

For example:

Dim a, b, x As Integer
If a = b Then
   Dim y As Integer = 20 ' Uniquely named block variable.
End If

<<<<<<<<<<<<<

Author

Commented:
Thank you.

Karl
Shahan AyyubSenior Software Engineer

Commented:
No problem! :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial