Results 1 to 9 of 9

Thread: Help Please

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    4

    Help Please

    Sorry, I know that is really basic, but I really need help with this. I'm trying to write a program and I'm having trouble in the section of code where I load the file. The file contains, on the first line, a number, telling half as many other lines there are. The other lines are pairs of questions and answers. Below, I'm trying to take the first line and save it to an integer called number. Then, I use that number to start a loop and put the questions and answers into an array called array. I don't seem to be reading anything. Do you think you can help me, please?


    Code:
    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
            state = 3
    
            TextBox1.Visible = True
            Button2.Visible = True
    
    
            Dim result As DialogResult
    
            OpenFileDialog1.ShowDialog()
    
            Dim filename As String = OpenFileDialog1.FileName
    
            If result = DialogResult.OK Then
                Dim read As New System.IO.StreamReader(filename)
    
                number = Val(read.ReadLine - vbNewLine)
    
                Dim counter As Integer
    
                For counter = 1 To number
                    array(counter, 1) = read.ReadLine
                    array(counter, 2) = read.ReadLine
                    counter = counter + 1
                Next
    
                read.Close()
            End If
    
            For counter = 1 To number
                todo(counter) = counter
            Next
    
            tempnum = 1
            wrongcount = 0
    
            TextBox1.Text = array(1, 1)
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help Please

    you need to rephrase your question so its clear + concise.
    explain what your code does + how we can help you improve it, + you'll get more answers

  3. #3
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: Help Please

    You would probably be better not having the 1st line contain the number of questions below it ... but rather work this out by the file size... this may be a better way to do it?

    vb Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.  
    5.     Private Class clsQuestion
    6.         Public Question As String
    7.         Public Answer As String
    8.         Public Sub New(ByVal Question As String, ByVal Answer As String)
    9.             Me.Question = Question
    10.             Me.Answer = Answer
    11.         End Sub
    12.     End Class
    13.  
    14.     Dim Filename as String = "C:\Windows\win.ini"
    15.     Dim Questions As New List(Of clsQuestion)
    16.  
    17.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    18.  
    19.         Dim objReader As StreamReader
    20.         objReader = New StreamReader(CStr(Filename))
    21.         Do Until objReader.EndOfStream
    22.             Dim line As String = objReader.ReadLine
    23.             If line.Contains("=") Then
    24.                 'this is a question so add it
    25.                 Dim QuArr = Split(line, "=", 2)
    26.                 Questions.Add(New clsQuestion(QuArr(0), QuArr(1)))
    27.             End If
    28.         Loop
    29.  
    30.         'Just to test that the questions were loaded
    31.         For Each item In Questions
    32.             MsgBox("Q: " & item.Question & vbCrLf & "A: " & item.Answer)
    33.         Next
    34.     End Sub
    35. End Class

    And your file would be (in the above case) structured like:

    Code:
    What day did God rest?=Sunday
    What color is the Sun?=Yellow
    What is my name?=Kris
    etc.

    Kris

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

    Re: Help Please

    This line:
    Code:
    number = Val(read.ReadLine - vbNewLine)
    is a problem. You can't subtract strings, nor do you need to. You don't need to remove a line break when you call ReadLine because it will inherently drop the line break. When you call ReadLine you will read up to the next line break, the line break is then skipped over and the file pointer placed at the beginning of the next line.

    Also, once you know how many lines there are, you need to create your array by specifying its size. You also need to be aware that arrays are zero-based, i.e. the first index is 0, not 1. There are other improvements to be made but, for a start:
    Code:
    number = Val(read.ReadLine())
    ReDim array(number - 1, 1)
    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
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help Please

    this might help:

    Code:
    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
            state = 3
    
            TextBox1.Visible = True
            Button2.Visible = True
    
    
            Dim result As DialogResult = OpenFileDialog1.ShowDialog()
    
            If result = DialogResult.OK Then
    
                Dim filename As String = OpenFileDialog1.FileName
    
                Dim read As New System.IO.StreamReader(filename)
    
                number = Val(read.ReadLine)
    
                Dim counter As Integer
    
                For counter = 1 To number
                    array(counter, 1) = read.ReadLine
                    array(counter, 2) = read.ReadLine
                    counter = counter + 1
                Next
    
                read.Close()
            End If
    
            For counter = 1 To number
                todo(counter) = counter
            Next
    
            tempnum = 1
            wrongcount = 0
    
            TextBox1.Text = array(1, 1)
        End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    4

    Re: Help Please

    Oh yeah, that was a desperate attempt to try to get it to work. It still didn't work when it was just "number = Val(read.ReadLine())" But thank you

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    4

    Re: Help Please

    Wow! Thank you very much! This is extremely helpful!

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    4

    Re: Help Please

    I really admire the way the members of this forum write code. It's incredible; I hope I get as good as you someday.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help Please

    i missed a mistake in that:

    Code:
    For counter = 1 To number
         array(counter, 1) = read.ReadLine
         array(counter, 2) = read.ReadLine
         counter = counter + 1 'the for loop increments your counter
    Next

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