|
-
Dec 12th, 2002, 05:42 AM
#1
Thread Starter
Member
Arrays (continued)
I thought I would start a new thread to clean up a little bit. It's too confusing. I've got code now that takes the first three characters of each line and lists it in a listbox. But...
How do I put each line into an array? The suggestions Edenis suggested in the other thread don't work (Add and ToArray are not members of System.Array)
Any suggestions are appreciated! Here is my code...
VB Code:
Private strIDNumbers As ArrayList()
Private intRecordcount As Integer = 0
Private Sub OpenFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileButton.Click
'determine if customers.txt exists
If System.IO.File.Exists("customers.txt") = True Then
'make the property labels visible
Me.IDNumberLabel.Visible = True
Me.NameLabel.Visible = True
Me.CreditLimitLabel.Visible = True
Me.BalanceDueLabel.Visible = True
'open the text file
Dim objStreamReader As System.IO.StreamReader
Dim strLine As String
objStreamReader = System.IO.File.OpenText("customers.txt")
Do While objStreamReader.Read
strLine = objStreamReader.ReadLine
Dim strID As String, intLength As Integer
intLength = Val(strLine.Length)
strID = strLine.Remove(3, (intLength - 3))
Me.IDListBox.Items.Add(strID)
intRecordcount = intRecordcount + 1
Loop
Me.OpenFileButton.Enabled = False
'customers.txt does not exist, stop
Else
MessageBox.Show("File does not exist!", "File not found", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub
-
Dec 12th, 2002, 08:26 AM
#2
Frenzied Member
VB Code:
'Private strIDNumbers As ArrayList() (What is this for??)
Private intRecordcount As Integer = 0
[b]Private names as New ArrayList()[/b]
Private Sub OpenFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileButton.Click
'determine if customers.txt exists
If System.IO.File.Exists("customers.txt") = True Then
'make the property labels visible
Me.IDNumberLabel.Visible = True
Me.NameLabel.Visible = True
Me.CreditLimitLabel.Visible = True
Me.BalanceDueLabel.Visible = True
'open the text file
Dim objStreamReader As System.IO.StreamReader
Dim strLine As String
objStreamReader = System.IO.File.OpenText("customers.txt")
Do While objStreamReader.Read
strLine = objStreamReader.ReadLine
[b] names.Add(strline)[/b]
Dim strID As String, intLength As Integer
intLength = Val(strLine.Length)
strID = strLine.Remove(3, (intLength - 3))
Me.IDListBox.Items.Add(strID)
intRecordcount = intRecordcount + 1
Loop
Me.OpenFileButton.Enabled = False
'customers.txt does not exist, stop
Else
MessageBox.Show("File does not exist!", "File not found", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub
What are you using strIDNumbers for?
BTW changes are in bold.
Dont gain the world and lose your soul
-
Dec 12th, 2002, 08:53 AM
#3
yay gay
like the code on the bottom said use ArrayList's and not arrays
\m/  \m/
-
Dec 12th, 2002, 09:35 AM
#4
PowerPoster
You can also use a data set to hold all the information from the file. I just read a section in my book that has the code. Here it is.
VB Code:
Dim ds As New DataSet()
Dim table As New DataTable("MyTable")
ds.Tables.Add("MyTable")
Dim counter As Integer
Dim col As DataColumn
' You only have three columns in your file, at least that is what I
' read in your previous post. If you had more columns of text to
' be seperated, you would change this number accordingly.
For counter = 0 to 2
col = New DataColumn("Column " & counter.ToString())
ds.Tables("MyTable").Columns.Add(col)
Next
Dim reader As New StreamReader("YourFilePathAndName")
Dim mystring As String
While reader.Peek <> -1
mystring = reader.ReadLine
ds.Tables("MyTable").Rows.Add(mystring.Split(","c))
'Instead of just splitting them, you might want to take off
'all leading and trailing spaces.
End While
-
Dec 12th, 2002, 01:46 PM
#5
Thread Starter
Member
Thanks for the help. I went with what DevGrp suggested, and it seems like it will work, but I have another problem. The line:
VB Code:
intLength = Val(strLine.Length)
crashes the program. I can't figure out why. It didn't do this before. The code that I have is exactly like DevGrp posted. Help!
-
Dec 12th, 2002, 01:59 PM
#6
I don't see a reference to the length of a string in DevGrp's code. Whare are you putting that?
Also I am assuming that intLength is an Integer in which case you don't need the Val() function around strLine.Length. Since The length property returns an integer, but that shouldn't be a problem if it is there.
-
Dec 12th, 2002, 02:10 PM
#7
Thread Starter
Member
It worked last night, and I don' t know what I changed for it not to. I need for it to remove the rest of the line after the first three characters. Should the code that I have work?
-
Dec 12th, 2002, 02:23 PM
#8
Post your code and we'll tell you.
-
Dec 12th, 2002, 02:39 PM
#9
Thread Starter
Member
I don't even have an array in the picture right now, all I'm trying to do is get it to add the stupid three digit ID numbers in the list box, and to do this I need to determine the length of each line, then cut off all but the first three characters of it. I had it working before, but don' t know what happened.
After I get that working, I will then need to assign each line into an array.
In a "SelectedValueChanged" procedure for the IDListBox, when an id is selected, it parses that line, and displays the values in labels.
VB Code:
Private Sub OpenFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileButton.Click
'determine if customers.txt exists
If System.IO.File.Exists("customers.txt") = True Then
'make the property labels visible
Me.IDNumberLabel.Visible = True
Me.NameLabel.Visible = True
Me.CreditLimitLabel.Visible = True
Me.BalanceDueLabel.Visible = True
'open customers.txt
Dim objStreamReader As System.IO.StreamReader
objStreamReader = System.IO.File.OpenText("customers.txt")
Dim strLine As String
Do While objStreamReader.Read
strLine = objStreamReader.ReadLine
Dim strID As String
Dim intLength As Integer
intLength = strLine.Length
strID = strLine.Remove(3, (strLine.Length - 3))
Me.IDListBox.Items.Add(strID)
'MessageBox.Show(strLine) 'this does work, and shows each line
Loop
'file does not exist
Else
MessageBox.Show("File does not exist!", "File not found", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub
-
Dec 12th, 2002, 02:44 PM
#10
It looks like it should work, but this part:
Dim intLength As Integer
intLength = strLine.Length
Doesn't do anything because you never refer to intLength again nor do you need to.
-
Dec 12th, 2002, 02:49 PM
#11
Thread Starter
Member
Oh yeah, that's right. I use to have the intLength variable in the line below in place of strLine.Length. That line, no matter what is in there (the variable or strLine.Length) crashes it out every time.
-
Dec 12th, 2002, 03:23 PM
#12
Thread Starter
Member
Ok, I figured out the problem, I had to change it to "Do Until objStreamReader.Peek = -1", that seemed to fix it. Now, any suggestions for the "SelectedValueChanged" procedure in the IDListBox????
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
|