dcsimg
Results 1 to 2 of 2

Thread: Streamreader and missing objects

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    685

    Streamreader and missing objects

    Hi Folks,

    In my app, I have an order form which pops up, on the order form loading, it reads in the contents of the file, splits it up and throws it into the listbox. it also adds the content to a list, which essentially holds all the contents of the file for processing.

    Code:
      Private Sub AddInvoiceItems()
            Using sr As StreamReader = File.OpenText("C:\OptoUpdate\OrderProductsInvoice.csv")
                Do While sr.Peek > 0
                    ProductExtract = sr.ReadLine.Split(",")
                    lstAvailableProductsUponReceipt.Items.Add(ProductExtract(1))
                    SC.FullOrderIUR.Add(sr.ReadLine)
                Loop
            End Using
    
            Using sr As StreamReader = File.OpenText("C:\OptoUpdate\OrderProductsRepeat.csv")
                Do While sr.Peek > 0
                    ProductExtract = sr.ReadLine.Split(",")
                    lstAvailableProducts.Items.Add(ProductExtract(1))
                    SC.FullOrderRecurring.Add(sr.ReadLine)
                Loop
            End Using
    
            MsgBox("Loading Complete")
            MsgBox(SC.FullOrderRecurring(0).ToString)
            MsgBox(SC.FullOrderRecurring(1).ToString)
            MsgBox(SC.FullOrderRecurring(2).ToString)
        End Sub
    the listbox itself, shows all entities. For example if the text file is the numbers 0-10 representing one number per line - I can read all ten lines into the listbox, and I can choose line 3.

    At the end I have the msgbox - showing the first entity in the list "Full Order Recurring" list. What I am expecting is the three message boxes is "0","1","2".

    What I actually get through, is the equivelant of "1","3","4" - clearly missing position 0 and 2.

    Anyone able to point out what is probably the obvious to anyone but me?

    Thanks
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: Streamreader and missing objects

    Quote Originally Posted by JayCR View Post
    the listbox itself, shows all entities. For example if the text file is the numbers 0-10 representing one number per line - I can read all ten lines into the listbox, and I can choose line 3.
    Are you sure about that?

    Every iteration of the loop, you execute sr.ReadLine twice. So you are reading 2 lines from the text file, part of the first line goes in the ListBox, and the second line is added to the List.

    That would explain why you are seeing the equivalent of "1","3","4" and missing 0 and 2 in the List, but the same should also be true for what's in the ListBox.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width