-2

I'm a novice at Visual basic so I need a little help getting started on a project I'm working on. I open a text file containing Student names and grades using a StreamReader but I want to select certain values from the text file (e.g. their highest grade, averages and overall marks). Would I be best off creating an array to display these values or writing functions to retrieve the data I want? I'm at a bit of a loss and don't want to waste my time so any suggestions would be very welcome.

data is stored as a txt file as follows student name,grade1,grade2,grade3,grade4,overall mark.

Thanks.

4

2 回答 2

1

我将创建一个具有所需属性的类,然后创建一个列表(对象)。请参阅下面的示例。

Public Sub GetStudentData()
    Dim oStudents As New List(Of Student)
    Using r As IO.StreamReader = New IO.StreamReader("file.txt")
        ' Store contents in this String.
        Dim line As String

        ' Read first line.
        line = r.ReadLine
        oStudents.Add(New Student(line))
        Do While (line IsNot Nothing)
            ' Read in the next line.
            line = r.ReadLine
            oStudents.Add(New Student(line))
        Loop
    End Using
End Sub

Public Class Student
    Public Property StudentID As String
    Public Property FirstName As String
    Public Property LastName As String
    Public Property HighestGrade As String
    Public Property Averages As String
    Public Property OverAllMarks As String

    Public Sub New(line As String)
        _StudentID = line ' Get specific string from text line
        _FirstName = line ' Get specific string from text line
        _LastName = line ' Get specific string from text line
        _HighestGrade = line ' Get specific string from text line
        _Averages = line ' Get specific string from text line
        _OverAllMarks = line ' Get specific string from text line
    End Sub
End Class
于 2013-03-27T13:35:19.573 回答
0

Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 btnAverage.Click

    Using myreader As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\Users\documents\studentresults.txt")
        myreader.TextFieldType = FileIO.FieldType.Delimited
        myreader.SetDelimiters(",")
        Dim currentrow As String()
        While Not myreader.EndOfData
            Try
                currentrow = myreader.ReadFields()
                Dim average As Double
                currentrow.Skip(1).Skip(2).Average(Function(s) Convert.ToDouble(s))
                MsgBox("Average is " & currentrow(0))
                Convert.ToString(average)
            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox("line" & ex.Message &
                       "is not valid and will be skipped")
            End Try
        End While
    End Using 

这是我到目前为止的代码,所以任何建议都会很棒。

于 2013-04-11T12:31:57.083 回答