|
-
May 12th, 2011, 11:47 PM
#1
Thread Starter
New Member
[RESOLVED] Add Text to Array
Hello, I am having an issue with my coding. I believe that this is correct, but when I try to load the form it results in an error. I am trying to load each individual line of the Cat1.txt file into the wordsarray that I have declared publicly because I will use the array in other subs. When I try this, I get a runtime error that states "NullReferenceException was unhandled." Is there a better way to code this? Also, how could I exclude the first line of the text file from the array?
Code:
Public Class MainForm
Dim wordsarray() As String
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim inFile As IO.StreamReader
Dim strInfo As String
Dim intSubscript As Integer = 0
If IO.File.Exists("Cat1.txt") Then
inFile = IO.File.OpenText("Cat1.txt")
Do Until inFile.Peek = -1
strInfo = inFile.ReadLine
wordsarray(intSubscript) = strInfo
intSubscript = intSubscript + 1
Loop
inFile.Close()
Else
MessageBox.Show("Can't find the Cat1.txt file", "Category 1 File Missing", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class
Any help would be appreciated.
-
May 13th, 2011, 07:15 AM
#2
Re: Add Text to Array
Check this code.
Code:
Public Class Form1
Dim wordsarray As New System.Collections.ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strInfo As String
If IO.File.Exists("Cat1.txt ") Then
Dim sr As New System.IO.StreamReader("Cat1.txt ")
strInfo = sr.ReadLine
While sr.Peek <> -1
strInfo = sr.ReadLine
wordsarray.Add(strInfo)
End While
Else
MessageBox.Show("Can't find the Cat1.txt file", "Category 1 File Missing", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class
-
May 13th, 2011, 10:01 AM
#3
Thread Starter
New Member
Re: [RESOLVED] Add Text to Array
Thank you so much, that worked perfectly!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|