Results 1 to 10 of 10

Thread: Reading to End of File Causing 1 Extra Date Piece

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Reading to End of File Causing 1 Extra Date Piece

    Hello. I have this piece of code:

    vb Code:
    1. Public Sub LoadContact()
    2.         With frmMain
    3.             If IO.File.Exists(Application.StartupPath & "\Info\Contacts.vis") Then
    4.                 Dim sr As New IO.StreamReader(Application.StartupPath & "\Info\Contacts.vis")
    5.                 While (sr.EndOfStream <> True)
    6.                     Dim sLine As String = sr.ReadLine
    7.                     Dim sSI() As String = Split(sLine, "|")
    8.                     sTitle.Add(sSI(0))
    9.                     sFullName.Add(sSI(1))
    10.                     sPhone.Add(sSI(2))
    11.                     sExt.Add(sSI(3))
    12.                     sFax.Add(sSI(4))
    13.                     sAddress1.Add(sSI(5))
    14.                     sAddress2.Add(sSI(6))
    15.                     sAddress3.Add(sSI(7))
    16.                     sCity.Add(sSI(8))
    17.                     sState.Add(sSI(9))
    18.                     sZipCode.Add(sSI(10))
    19.                     sEmail.Add(sSI(11))
    20.                     ContactInfo.Add(sTitle(iCount).ToString & "|" & sFullName(iCount).ToString & "|" & _
    21.                                     sPhone(iCount).ToString & "|" & sExt(iCount).ToString & "|" & _
    22.                                     sFax(iCount).ToString & "|" & sAddress1(iCount).ToString & "|" & _
    23.                                     sAddress2(iCount).ToString & "|" & sAddress3(iCount).ToString & "|" & _
    24.                                     sCity(iCount).ToString & "|" & sState(iCount) & "|" & _
    25.                                     sZipCode(iCount).ToString & "|" & sEmail(iCount).ToString)
    26.                     .lstContacts.BeginUpdate()
    27.                     .lstContacts.Items.Clear()
    28.                     For i As Integer = 0 To UBound(ContactInfo.ToArray)
    29.                         .lstContacts.Items.Add(sFullName(i))
    30.                         .lstvwContacts.Items.Add(sTitle(i))
    31.                         Dim lstitm1, lstitm2, lstitm3 As New ListViewItem.ListViewSubItem
    32.                         lstitm1.Text = sFullName(i)
    33.                         lstitm2.Text = sAddress1(i)
    34.                         lstitm3.Text = sEmail(i)
    35.                         .lstvwContacts.Items(iCount).SubItems.AddRange(New ListViewItem.ListViewSubItem() {lstitm1, lstitm2, lstitm3})
    36.                     Next
    37.                     .lstContacts.EndUpdate()
    38.                     iCount += 1
    39.                 End While
    40.                 sr.Close()
    41.             End If
    42.         End With
    43.     End Sub

    So what it does, is it reads the contents of a text file and then loads all the info into a listarray, then I add those contents into a listview. Whats happening is:

    say I have

    123 123 123 123
    1234 1234 1234 1234

    in my Text File

    When It loads to my ListView it does this

    123 123 123 123
    1234 1234 1234 1234
    1234

    It adds the Last Item Twice, but, only in the first column.

    Whats my Issue and how can I fix this?

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Reading to End of File Causing 1 Extra Date Piece

    This probably isnt the issue but personally when I am doing For loops I use something like this:
    vb Code:
    1. For i As Integer = 0 To CollectionName.Length - 1
    instead of what you are doing which is:
    vb Code:
    1. For i As Integer = 0 to UBound(CollectionName)
    Might be worth trying to change it? I dunno...
    its kind of hard to work out what on earth your app is doing as there is so much going on there. Is there any way you could chop it down a bit but without taking out anything important just so that its easier for us to look through?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Reading to End of File Causing 1 Extra Date Piece

    vb Code:
    1. While (sr.EndOfStream <> True)
    2.                     Dim sLine As String = sr.ReadLine
    3.                     Dim sSI() As String = Split(sLine, "|")
    4.                     sTitle.Add(sSI(0))
    5.                     sFullName.Add(sSI(1))
    6.                     sPhone.Add(sSI(2))
    7.                     sExt.Add(sSI(3))
    8.                     sFax.Add(sSI(4))
    9.                     sAddress1.Add(sSI(5))
    10.                     sAddress2.Add(sSI(6))
    11.                     sAddress3.Add(sSI(7))
    12.                     sCity.Add(sSI(8))
    13.                     sState.Add(sSI(9))
    14.                     sZipCode.Add(sSI(10))
    15.                     sEmail.Add(sSI(11))
    16.                     ContactInfo.Add(sTitle(iCount).ToString & "|" & sFullName(iCount).ToString & "|" & _
    17.                                     sPhone(iCount).ToString & "|" & sExt(iCount).ToString & "|" & _
    18.                                     sFax(iCount).ToString & "|" & sAddress1(iCount).ToString & "|" & _
    19.                                     sAddress2(iCount).ToString & "|" & sAddress3(iCount).ToString & "|" & _
    20.                                     sCity(iCount).ToString & "|" & sState(iCount) & "|" & _
    21.                                     sZipCode(iCount).ToString & "|" & sEmail(iCount).ToString)

    What that Part Does is It will LOOP Through the Text FIle till End Of File, then I have Defined ArrayList sFullName all the way to sEmail and then It will Split out the "|" that are in the Text File.

    It then adds all the Items that I split in to 1 ArrayList, ContactInfo.

    vb Code:
    1. For i As Integer = 0 To ContactInfo.ToArray.Length - 1
    2.                         .lstContacts.Items.Add(sFullName(i))
    3.                         .lstvwContacts.Items.Add(sTitle(i))
    4.                         Dim lstitm1, lstitm2, lstitm3 As New ListViewItem.ListViewSubItem
    5.                         lstitm1.Text = sFullName(i)
    6.                         lstitm2.Text = sAddress1(i)
    7.                         lstitm3.Text = sEmail(i)
    8.                         .lstvwContacts.Items(iCount).SubItems.AddRange(New ListViewItem.ListViewSubItem() {lstitm1, lstitm2, lstitm3})
    9.                     Next


    This loops through all ContactInfo (The ArrayList, I added all Information Too) To get the Lowest through the Highest Info, then It will add them to a ListBox, which is .lstContacts, and a ListView which is .lstvwContacts, My Problem is .lstvwContacts.Items.Add(sTitle(i)) seems to be adding 1 more Title than Everything Else. So, example:

    Mr Wesley 008
    Mr. Wes 0008
    Mr.

    The spaces are say a ListView Detail View in WinForm

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reading to End of File Causing 1 Extra Date Piece

    Have you debugged your code? Set a breakpoint at the top and then step through it line by line. It will likely become clear when you do that because you can test the state before and after every step.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Reading to End of File Causing 1 Extra Date Piece

    Hi, I did set break points, and I found out the Next was Looping Twice only @ the last bit, but I can't figure out why, this is bugging me -.- I also tried doing a For Each Loop but can't get the right control for a ArrayList Item.

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Reading to End of File Causing 1 Extra Date Piece

    If ContactInfo is an ArrayList then why are you using ContactInfo.ToArray in the loop :S
    Why not just:
    vb Code:
    1. For i As Integer = 0 To ContactInfo.Length - 1
    I dont know if that will make any difference to your problem (i doubt it) but just thought it might be worth mentioning

    Also when you say you cant get the right control for an ArrayList item, the ArrayList will hold items of whatever type you added to it. So if you added a load of strings to the ArrayList then you would do:
    vb Code:
    1. For Each something As String In MyArrayList
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Reading to End of File Causing 1 Extra Date Piece

    Still doesn't fix my issue.


    vb Code:
    1. For i As Integer = 0 To ContactInfo.ToArray.Length - 1
    2.                         .lstContacts.Items.Add(sFullName(i))
    3.                         .lstvwContacts.Items.Add(sTitle(i))
    4.                         Dim lstitm1, lstitm2, lstitm3 As New ListViewItem.ListViewSubItem
    5.                         lstitm1.Text = sFullName(i)
    6.                         lstitm2.Text = sAddress1(i)
    7.                         lstitm3.Text = sEmail(i)
    8.                         .lstvwContacts.Items(iCount).SubItems.AddRange(New ListViewItem.ListViewSubItem() {lstitm1, lstitm2, lstitm3})
    9.                     Next

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Reading to End of File Causing 1 Extra Date Piece

    How can you say it doesnt fix your issue, then post code that doesnt implement either of the things I suggested?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Reading to End of File Causing 1 Extra Date Piece

    For i As Integer = 0 To ContactInfo.ToArray.Length - 1

    I needed to add .ToArray because .Length wasn't showing up on Contact Info.


    The For Each i as String In ContactInfo didn't work, because I couldn't be converted to a INteger, and then if I did For Each i as Integer In ContactInfo, it said the TEXT Like the Example Text couldn't be converted to a String. I tried both ways, its not working.

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Reading to End of File Causing 1 Extra Date Piece

    Ah sorry its .Count for an ArrayList, not .Length, so you could just do
    vb Code:
    1. For i As Integer = 0 To ContactInfo.Count - 1
    Anyway that shouldnt be making any difference.

    I think what you need to do is step through your code in the debugger as JMC mentioned. Use breakpoints and the Step Into function (F8 key usually does this) and check to see why your ContactInfo collection has one more item in it than you are expecting. It must have one extra item for it to execute the loop an extra time. If you step through line by line and check the contents of the ContactInfo list constantly you can see when this additional item is added to it. Maybe someone with a little more experienc than me can tell just by looking at your code where you are going wrong but I think without seeing your entire app the only thing I can suggest is to step through it and see whats going on.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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