|
-
Sep 16th, 2009, 05:45 PM
#1
Thread Starter
New Member
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
-
Sep 16th, 2009, 08:50 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 16th, 2009, 08:53 PM
#3
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:
Imports System.IO
Public Class Form1
Private Class clsQuestion
Public Question As String
Public Answer As String
Public Sub New(ByVal Question As String, ByVal Answer As String)
Me.Question = Question
Me.Answer = Answer
End Sub
End Class
Dim Filename as String = "C:\Windows\win.ini"
Dim Questions As New List(Of clsQuestion)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objReader As StreamReader
objReader = New StreamReader(CStr(Filename))
Do Until objReader.EndOfStream
Dim line As String = objReader.ReadLine
If line.Contains("=") Then
'this is a question so add it
Dim QuArr = Split(line, "=", 2)
Questions.Add(New clsQuestion(QuArr(0), QuArr(1)))
End If
Loop
'Just to test that the questions were loaded
For Each item In Questions
MsgBox("Q: " & item.Question & vbCrLf & "A: " & item.Answer)
Next
End Sub
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
-
Sep 16th, 2009, 08:54 PM
#4
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)
-
Sep 16th, 2009, 08:56 PM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 17th, 2009, 04:15 PM
#6
Thread Starter
New Member
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
-
Sep 17th, 2009, 04:18 PM
#7
Thread Starter
New Member
Re: Help Please
Wow! Thank you very much! This is extremely helpful!
-
Sep 17th, 2009, 04:20 PM
#8
Thread Starter
New Member
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.
-
Sep 17th, 2009, 08:09 PM
#9
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|