help with a vb problem

topic posted Sat, November 19, 2005 - 3:18 PM by  cowboyangel
Share/Save/Bookmark
Advertisement
I am a beginning student in vb.got this problem that's hanging me up. I have to search a txt file with 20 array elemnts in it called "scores.txt" in the Bin folder, then list a count of all number of students who got that grade. the line in my code that debug is stopping on is
intStudentScore(19) = Convert.ToInt32(sreStreamReader.ReadLine())

what is wrong with this assignment statement or why won't this program work. When I type 72 in the input text, I should see the number 3, because that's how many students got a 72. Help

here's my code:

'Project name: Scores Project
'Project purpose: The project displays the number of students earning
' a specific score.
'Created/revised by: <*************>

Option Explicit On
Option Strict On

Public Class frmScores
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.Container

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnExit As System.Windows.Forms.Button
Friend WithEvents btnDisplay As System.Windows.Forms.Button
Friend WithEvents txtEnterScore As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnExit = New System.Windows.Forms.Button
Me.btnDisplay = New System.Windows.Forms.Button
Me.txtEnterScore = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'btnExit
'
Me.btnExit.Anchor = System.Windows.Forms.AnchorStyles.None
Me.btnExit.Location = New System.Drawing.Point(64, 152)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing.Size(121, 32)
Me.btnExit.TabIndex = 1
Me.btnExit.Text = "E&xit"
'
'btnDisplay
'
Me.btnDisplay.Location = New System.Drawing.Point(64, 104)
Me.btnDisplay.Name = "btnDisplay"
Me.btnDisplay.Size = New System.Drawing.Size(121, 32)
Me.btnDisplay.TabIndex = 0
Me.btnDisplay.Text = "&Display"
'
'txtEnterScore
'
Me.txtEnterScore.Location = New System.Drawing.Point(64, 56)
Me.txtEnterScore.Name = "txtEnterScore"
Me.txtEnterScore.Size = New System.Drawing.Size(120, 27)
Me.txtEnterScore.TabIndex = 2
Me.txtEnterScore.Text = ""
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Tahoma", 9.0!,

System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(64, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(120, 23)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Enter the Test Score"
'
'frmScores
'
Me.AutoScaleBaseSize = New System.Drawing.Size(8, 20)
Me.ClientSize = New System.Drawing.Size(252, 203)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtEnterScore)
Me.Controls.Add(Me.btnExit)
Me.Controls.Add(Me.btnDisplay)
Me.Font = New System.Drawing.Font("Tahoma", 12.0!,

System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name = "frmScores"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Final Exam"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Handles btnExit.Click
'ends the application
Me.Close()
End Sub

Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles btnDisplay.Click

Dim intStudentScore() As Integer ' array element scores
Dim intCount As Integer ' counter variable
Dim intSearchGrades As Integer ' number of students to search for same grade
Dim sreStreamReader As IO.StreamReader

intSearchGrades = Convert.ToInt32(Me.txtEnterScore.Text)
'open the scores file
If IO.File.Exists("scores.txt") Then
sreStreamReader = IO.File.OpenText("scores.txt")
intStudentScore(19) = Convert.ToInt32(sreStreamReader.ReadLine())


For Each intSearchGrades In intStudentScore
If intSearchGrades = intStudentScore(intCount) Then
intCount = intCount + 1
End If

Next intSearchGrades

MessageBox.Show("There are," & intCount & "," & "students", "Students, who

got that grade", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else

MessageBox.Show("File does not exist,", "File does not exist", _
MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

End Sub


End Class
Advertisement
Advertisement
  • Re: help with a vb problem

    Tue, November 22, 2005 - 9:05 AM
    what does the file look like that you're reading?


    Also -- if you delete the windows-form-generated code, the code snippets you post will be a slot shorter.
    • Re: help with a vb problem

      Tue, November 22, 2005 - 9:55 AM
      It sounds to me like the last line in the file may have a blank space. What you should always do is verify that data coming in, especially from something such as a file, is in the format you are expecting.

      For example:

      Dim strLine As String = sreStreamReader.ReadLine()

      If IsNumeric(strLine) Then
      intStudentScore(19) = Convert.ToInt32 (sreStreamReader.ReadLine())

      Else
      MsgBox "Line 20 is not numeric"
      ' Throw exception or exit here.
      End IF


      Hope that helps
      • Re: help with a vb problem

        Tue, November 22, 2005 - 1:43 PM
        Hey thanks psychotty. That was good advice. I ended up transfering the file contents (which was in a single column array) to the Subscripts in the assignment.... that worked too. Thanks again.