-1

I was wondering if there was a way to read multiple lines of comma delimited text from multiple text files and store them all in the same array without overwriting the data that is already there? I've tried multiple things, nothing seems to be working. So, for example:

Dim reader As TextFieldParser = New TextFieldParser("text1.txt")
 reader.TextFieldType = FieldType.Delimited
 reader.SetDelimiters(",")
Dim currentrow as string() = reader.ReadFields()
Dim storageArray() as string()
storageArray(0) = currentrow(0)
storageArray(1) = currentrow(1)
currentrow = reader.ReadFields()
storageArray(2) = currentrow(0)
storageArray(3) = currentrow(1)
Dim reader As TextFieldParser = New TextFieldParser("text2.txt")
 reader.TextFieldType = FieldType.Delimited
 reader.SetDelimiters(",")
 currentrow =  reader.ReadFields()
 storageArray(4) = currentrow(0)
 storageArray(4) = currentrow(1)
 ..... etc., etc.

I keep getting System.NullReferenceException thrown when I try assigning one value of an array into a different array...

I am aware that ReadFields() returns an array, so I've also tried storing each line in a jagged array, which throws the same error.

As an add on thought to this, is there a better way for me to be doing this? I read a lot about reading the whole file into an array and then parsing it, but I still seem to be having trouble storing a value of one array in another...

Thank you all in advance for the help!

1
  • Use a List(of string) Commented Feb 20, 2015 at 22:02

1 Answer 1

0

To declare an array of string the correct syntax is the following

Dim storageArray(size) as String

where size is the number of elements that you want to store in that array.
So your code above doesn't compile at all.

However in your context you don't know how big this array need to be because you haven't read the file. In this scenario the best approach is to use a List(Of String) that allows to add new elements to the List without the need to redimension the array

So you code could be written as

 Dim storageList = New List(Of String)()
 Using reader As TextFieldParser = New TextFieldParser("text1.txt")
     reader.TextFieldType = FieldType.Delimited
     reader.SetDelimiters(",")
     storageList.AddRange(reader.ReadFields())
 End Using
 Using reader As TextFieldParser = New TextFieldParser("text2.txt")
     reader.TextFieldType = FieldType.Delimited
     reader.SetDelimiters(",")
     storageList.AddRange(reader.ReadFields())
 End Using

 ....

Note that a disposable object like the TextFieldParser need to be disposed when you finish to use it. So enclude everything in a Using Statement

You could use a List(Of strig) to loop over data

 for each data in storageList
     Console.WriteLine(data)
 next

or use a single element like an array

Dim data = storageList(0)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply steve. I'll have to look into List(Of String) a bit more, thats a new one for me. But what I've found (figures after messing with this for three days that i'd find a solution after I post on stackoverflow lol) is that I can use: Array.ConstraintedCopy(array1, 0, array2, 0, array1.Length) which will copy the contents of array1, starting at index 0 into array 2, starting at index 0 and go until the length of array1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.