Results 1 to 12 of 12

Thread: Arrays (continued)

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60

    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:
    1. Private strIDNumbers As ArrayList()
    2.     Private intRecordcount As Integer = 0
    3.  
    4.     Private Sub OpenFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileButton.Click
    5.         'determine if customers.txt exists
    6.         If System.IO.File.Exists("customers.txt") = True Then
    7.             'make the property labels visible
    8.             Me.IDNumberLabel.Visible = True
    9.             Me.NameLabel.Visible = True
    10.             Me.CreditLimitLabel.Visible = True
    11.             Me.BalanceDueLabel.Visible = True
    12.             'open the text file
    13.             Dim objStreamReader As System.IO.StreamReader
    14.             Dim strLine As String
    15.             objStreamReader = System.IO.File.OpenText("customers.txt")
    16.             Do While objStreamReader.Read
    17.                 strLine = objStreamReader.ReadLine
    18.  
    19.                 Dim strID As String, intLength As Integer
    20.                 intLength = Val(strLine.Length)
    21.                 strID = strLine.Remove(3, (intLength - 3))
    22.                 Me.IDListBox.Items.Add(strID)
    23.                 intRecordcount = intRecordcount + 1
    24.             Loop
    25.             Me.OpenFileButton.Enabled = False
    26.             'customers.txt does not exist, stop
    27.         Else
    28.                 MessageBox.Show("File does not exist!", "File not found", _
    29.                 MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    30.  
    31.         End If
    32.     End Sub

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    VB Code:
    1. 'Private strIDNumbers As ArrayList() (What is this for??)
    2. Private intRecordcount As Integer = 0
    3. [b]Private names as New ArrayList()[/b]
    4.  
    5. Private Sub OpenFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileButton.Click
    6.         'determine if customers.txt exists
    7.         If System.IO.File.Exists("customers.txt") = True Then
    8.             'make the property labels visible
    9.             Me.IDNumberLabel.Visible = True
    10.             Me.NameLabel.Visible = True
    11.             Me.CreditLimitLabel.Visible = True
    12.             Me.BalanceDueLabel.Visible = True
    13.             'open the text file
    14.             Dim objStreamReader As System.IO.StreamReader
    15.             Dim strLine As String
    16.             objStreamReader = System.IO.File.OpenText("customers.txt")
    17.             Do While objStreamReader.Read
    18.                 strLine = objStreamReader.ReadLine
    19.                [b] names.Add(strline)[/b]
    20.                
    21.                 Dim strID As String, intLength As Integer
    22.                 intLength = Val(strLine.Length)
    23.                 strID = strLine.Remove(3, (intLength - 3))
    24.                 Me.IDListBox.Items.Add(strID)
    25.                 intRecordcount = intRecordcount + 1
    26.             Loop
    27.             Me.OpenFileButton.Enabled = False
    28.             'customers.txt does not exist, stop
    29.         Else
    30.                 MessageBox.Show("File does not exist!", "File not found", _
    31.                 MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    32.  
    33.         End If
    34. End Sub
    What are you using strIDNumbers for?
    BTW changes are in bold.
    Dont gain the world and lose your soul

  3. #3
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    like the code on the bottom said use ArrayList's and not arrays
    \m/\m/

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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:
    1. Dim ds As New DataSet()
    2. Dim table As New DataTable("MyTable")
    3.  
    4. ds.Tables.Add("MyTable")
    5.  
    6. Dim counter As Integer
    7. Dim col As DataColumn
    8.  
    9. ' You only have three columns in your file, at least that is what I
    10. ' read in your previous post.  If you had more columns of text to
    11. ' be seperated, you would change this number accordingly.
    12. For counter = 0 to 2
    13.     col = New DataColumn("Column " & counter.ToString())
    14.     ds.Tables("MyTable").Columns.Add(col)
    15. Next
    16.  
    17. Dim reader As New StreamReader("YourFilePathAndName")
    18. Dim mystring As String
    19.  
    20. While reader.Peek <> -1
    21.     mystring = reader.ReadLine
    22.     ds.Tables("MyTable").Rows.Add(mystring.Split(","c))
    23.     'Instead of just splitting them, you might want to take off
    24.     'all leading and trailing spaces.
    25. End While

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    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:
    1. 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!

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    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?

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Post your code and we'll tell you.

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    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:
    1. Private Sub OpenFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileButton.Click
    2.         'determine if customers.txt exists
    3.         If System.IO.File.Exists("customers.txt") = True Then
    4.             'make the property labels visible
    5.             Me.IDNumberLabel.Visible = True
    6.             Me.NameLabel.Visible = True
    7.             Me.CreditLimitLabel.Visible = True
    8.             Me.BalanceDueLabel.Visible = True
    9.             'open customers.txt
    10.             Dim objStreamReader As System.IO.StreamReader
    11.             objStreamReader = System.IO.File.OpenText("customers.txt")
    12.             Dim strLine As String
    13.             Do While objStreamReader.Read
    14.                 strLine = objStreamReader.ReadLine
    15.                 Dim strID As String
    16.                 Dim intLength As Integer
    17.                 intLength = strLine.Length
    18.                 strID = strLine.Remove(3, (strLine.Length - 3))
    19.                 Me.IDListBox.Items.Add(strID)
    20.                 'MessageBox.Show(strLine) 'this does work, and shows each line
    21.             Loop
    22.             'file does not exist
    23.         Else
    24.                 MessageBox.Show("File does not exist!", "File not found", _
    25.                 MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    26.         End If
    27.     End Sub

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    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.

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    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
  •  



Click Here to Expand Forum to Full Width