VideoHelp Forum




+ Reply to Thread
Results 1 to 8 of 8
  1. Member
    Join Date
    Oct 2013
    Location
    Ireland
    Search PM
    I'm trying to use stream reader to read a text file in visual basic, but it wont work... The code i'm using is :
    Dim sr As IO.StreamReader = _
    IO.File.OpenText("GROWTH.TXT")


    Dim Occupation As String = ""
    Dim YearOne As Integer = 0
    Dim YearTwo As Integer = 0
    Dim Increase As Integer = 0
    Dim fmtStr As String = "{0,-20}{1,-20}{2,-20}{3,-20}"


    lstResults.Items.Add(String.Format(fmtStr, "Occupation", "YearOne", "YearTwo", "Increase"))
    Do While sr.Peek <> -1
    Occupation = sr.ReadLine
    YearOne = CDbl(sr.ReadLine)
    YearTwo = CDbl(sr.ReadLine)



    Is there something wrong here?
    Quote Quote  
  2. Member DB83's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Search Comp PM
    Well my knowledge of vb ended before OO programming became the forte but it does appear that you are not providing for 'Increase'.

    And how would you run the loop since it does not have any end pointers ?
    Quote Quote  
  3. Member
    Join Date
    Oct 2013
    Location
    Ireland
    Search PM
    I have loop and sr.close at the end, I don't know what to write for increase?
    Quote Quote  
  4. Member DB83's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Search Comp PM
    Surely if 'increase' is the same type as the other variables, your code becomes

    .......
    Increase = CDbl(sr.ReadLine)
    Loop

    etc.
    Quote Quote  
  5. Member
    Join Date
    Oct 2013
    Location
    Ireland
    Search PM
    I tried that, it says, InvalidCastException was unhandled...
    Quote Quote  
  6. Member DB83's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Search Comp PM
    Your questions would probably be better addressed at a dedicated forum.

    But I can well remember when I was dabbling with vb that it often took minutes to write the code and hours to debug it. Often the fault did not lay where you thought it was.

    There could even be fault in the text file. Without seeing that and the whole code we can but guess. Like I said, you would best be served at a dedicated forum.
    Quote Quote  
  7. Member bendixG15's Avatar
    Join Date
    Aug 2004
    Location
    United States
    Search Comp PM
    Sorry, can't help, all I can say is that you brought back a lot of memories from my VB days.
    Good luck
    Quote Quote  
  8. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    First glance there appears to be a lot of information missing. You dim YearOne as Integer and then later try to make it equal a line that's converted to a CDBL (CDbl function converts an expression to type Double). It is possible that you don't get at least one error from Converting a LINE to a DOUBLE to an INTEGER but without the specifics of the file you are reading its hard to say.

    What exactly are you trying to accomplish from what data?
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!