FileInfo Array Problem

Doug

Active member
Joined
Oct 5, 2011
Messages
44
Programming Experience
Beginner
This morning is just not my morning. This seems pretty straight forward but for some reason I just can't get the syntax correct.

Here are the lines of code that I am having issue with:

VB.NET:
Private _moveFiles() As FileInfo

'Inside the subroutine the code follows
Dim counter As Integer
Dim pathHold As String

If IO.File.Exists(pathHold) Then
            Dim fileHold As New FileInfo(pathHold)
            _moveFiles(counter) = fileHold
End If

I am getting "Object reference not set to an instance of an object." error on the line: _moveFiles(counter) = fileHold

Normally I would initialize a FileInfo variable using "New" but New cannot be used with an array which is why I tried creating a new FileInfo then making an element in the array equal to it.

How do I assign specific paths to my FileInfo array?
 
Nevermind. I found the problem. _moveFiles() did not have any elements in it. I initialized it with 0 elements and it works now.

This is one of those bad morning for me. I think I'll go for a walk to clear my head.
 
You really shouldn't be using an array in that situation. Arrays are not resizable (despite what you might think, the appearance of resizing an array actually involves creating a new array and copying the elements from one to the other) so if you intend to be adding and/or removing items then you should be using a collection. That code should read:
Private _moveFiles As New List(Of FileInfo)

'Inside the subroutine the code follows
Dim counter As Integer
Dim pathHold As String

If IO.File.Exists(pathHold) Then
            Dim fileHold As New FileInfo(pathHold)
            _moveFiles.Add(fileHold)
End If
 
Back
Top