Get values from listarray VB NET 2008
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Get values from listarray VB NET 2008

  1. #1
    Join Date
    Aug 2009
    Posts
    1

    Get values from listarray VB NET 2008

    Hi everyone, I have to make a program that reads a txt file after reading it, show it in a richtext box and in another tab make the average of the grades the input received sort it by the student id and then in the same rtb sort it by the average of their grades, and finally save it on a new text file.

    The text file input is something like this

    12 3 10.0 6.9 7.3
    19 2 6.7 9.3
    10 3 4.5 9.3 4.5

    and the output should look like this

    sorted by student ID
    10 6.1 4.5 9.3 4.5
    12 8.1 10.0 6.9 7.3
    19 5.3 6.7 9.3

    sorted by grade average
    12 8.1 10.0 6.9 7.3
    10 6.1 4.5 9.3 4.5
    19 5.3 6.7 9.3

    As you noticed in the input the first number is the student ID, the second number is the number of tests made by the student, and the other 3 numbers are grades, in the output the first number is the student ID, the second number is the average of the tests, and the other 3 numbers are grades, but my problem is that I can't get the values from the list that I 've created, here is the code:

    Code:
          Imports System.IO
    
          Public Class Form1
    
              Private Sub AbrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AbrirToolStripMenuItem.Click
    
                  ' Try
    
                  Dim txtString As String()
    
                  txtString = System.IO.File.ReadAllLines("C:\read.txt")
    
                  ' Catch ioexception As IOException
    
                  'MsgBox("File Not Found")
    
                  Dim studentList As New System.Collections.Generic.Dictionary(Of Integer, Decimal())
     
                  For Each line As String In txtString
     
                      If (String.IsNullOrEmpty(line)) Then
     
                          Continue For
    
                      End If
    
                      Dim data As String() = line.Split(" ")
     
                      Dim studentId As Integer = Convert.ToInt32(data(0))
    
                      Dim gradeList As New List(Of Decimal)
           
    
                      For i1 As Integer = 1 To data.Length - 1
    
                          gradeList.Add(Convert.ToDecimal(data(i1)))
           
    
                      Next
           
                      studentList.Add(studentId, gradeList.ToArray())      
    
                  Next
    
                  'At this point I have it all in memory :)
           
    
                 'this code should retrieve me the values but it doesn't
    
                 'the error says that the key has not been found on the dictionary
           
    
                  Dim value1 As Object
    
                  value1 = studentList(1)
    
                  RichTextBox1.Text = value1
     
                  'End Try
      
              End Sub
    thanks in advance I really appreciate all your help

  2. #2
    Join Date
    Aug 2009
    Posts
    12

    Re: Get values from listarray VB NET 2008

    I hope you don't mind if I rewrite the code for definition interpretation.

    In the program, you should use the following approach in writing this program.
    1. Load a file definition (which you have done)
    2. Declare a Student Class definition
    3. Define the sorting ability of the class (IComparable)
    4. Read the definition into student class list

    According to the guidelines, i have written the class definition as followed.
    Class Definition
    Code:
    Public Class Student : Implements IComparable(Of Student)
            Public StudentID As Integer
            Public ReadOnly Property TestCount() As Integer
                Get
                    Return Grades.Count
                End Get
            End Property
            Public Grades As New List(Of Decimal)
            Public ReadOnly Property AverageGrade() As Decimal
                Get
                    Return Math.Round(Grades.Average(), 1)
                End Get
            End Property
    
            Public Sub New(ByVal def As String)
                Dim tmp As String() = def.Split(" ")
                If tmp.Length = 0 Then Throw New ApplicationException("The student definition cannot be empty.")
    
                StudentID = Integer.Parse(tmp(0))
                Dim count As Integer = Integer.Parse(tmp(1)) 'tmp(1) is the test count, use it for validation only
                For n = 2 To tmp.Length - 1
                    Grades.Add(Decimal.Parse(tmp(n)))
                Next
    
                If Not count = Grades.Count Then Throw New ApplicationException("The number of test and grades does not match")
            End Sub
    
            Public Function GetResult() As String
                Dim tmp1 As String = StudentID & " " & AverageGrade.ToString()
                Dim tmp2 As String = ""
    
                For n As Integer = 0 To Grades.Count - 1
                    If n = 0 Then
                        tmp2 = Grades(n)
                    Else
                        tmp2 &= " " & Grades(n)
                    End If
                Next
    
                Return tmp1 & " " & tmp2
            End Function
    
            Public Function CompareTo(ByVal other As Student) As Integer Implements System.IComparable(Of Student).CompareTo
                Return StudentID.CompareTo(other.StudentID)
            End Function
        End Class
    
        Public Class StudentGradeComparer : Implements IComparer(Of Student)
            Public Function Compare(ByVal x As Student, ByVal y As Student) As Integer Implements System.Collections.Generic.IComparer(Of Student).Compare
                Return x.AverageGrade.CompareTo(y.AverageGrade)
            End Function
        End Class

    Also, to use this class, you should create 3 textboxes and 3 buttons.

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim students As New List(Of Student)
    
            'Read input lines
            For Each line In TextBox1.Lines
                students.Add(New Student(line))
            Next
    
            'Sort By Student ID
            students.Sort()
            TextBox2.Text = ""
            For n As Integer = 0 To students.Count - 1
                TextBox2.Text &= students(n).GetResult() & vbCrLf
            Next
    
            'Sort By Average Grade
            students.Sort(New StudentGradeComparer())
            TextBox3.Text = ""
            For n As Integer = students.Count - 1 To 0 Step -1
                TextBox3.Text &= students(n).GetResult() & vbCrLf
            Next
        End Sub
    By the way, you have got a mistake in the example (You divided the total score by 3, which should be 2 !)
    19 5.3 6.7 9.3 should be 19 8 6.7 9.3

    This program use Visual Basic 2008 functionality, therefore, it should be opened by Visual Basic 2008.

    I have attached the code for you for your reference. Hope that i can help you ^.^.
    Attached Files Attached Files
    Last edited by chkmos; August 29th, 2009 at 08:03 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center