Results 1 to 3 of 3

Thread: [2005] Urgent: Read text from files and save contents in a string array

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Smile [2005] Urgent: Read text from files and save contents in a string array

    Hi,

    I am trying to read from a number of files and trying to save the contents of each file in an array of strings. The error that I keep receiving is: Object reference not set to an instance of an object.

    Public Sub Read(ByVal template As String(), ByVal newfiles As String())
    Try
    Dim strfilename As String
    Dim temp_only_for_appending As String()
    Dim count As Integer = 0

    'Looping through each file in the the string array passed

    For Each file As String In template

    'Read all text from each template file
    strfilename = System.IO.File.ReadAllText(file)
    'Concetanate the strings and copy it to an array item

    '********THIS IS WHERE IT FAILS******
    temp_only_for_appending(count) = (strinputstring & vbCrLf & vbCrLf & strfilename)
    count += 1 'Increment the array counter
    Next

    Catch E As Exception
    ' Let the user know what went wrong.
    MsgBox("The file could not be read: ")
    MsgBox(E.Message)
    End Try

    End Sub

    Please tell me what I am doing incorrectly so that I can fix my code. I need some help urgently. Thanks in advance.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Urgent: Read text from files and save contents in a string array

    Adding this line right above the line that causes error should take care of the exception.
    VB Code:
    1. ReDim Preserve temp_only_for_appending(count)

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Thumbs up Resolved Re: [2005] Urgent: Read text from files and save contents in a string array

    Thanks a lot for your quick reply man!! I read up on the ReDim statement and it worked!! I did not know the dynamic aspects of an array need to be re-specified in this fashion. Anyways, thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width