|
-
Mar 7th, 2007, 09:41 PM
#1
Thread Starter
Junior Member
[RESOLVED] [02/03] Passing Array of Structures to Functions
Everytime I try to debug this code, I get an error in the function saying that an instance of the object is not created. I really do not know why or what I am doing wrong. I stare, but I do not know what is wrong. I am not familiar with structures, but I am trying.
Code:
Const MAXSIZE As Integer = 25
'MY STRUCTURE for the text file!
Structure StudentData
Dim name As String
Dim ssn As String
Dim numGrades As Double 'Gathers of the number of grades of each student
Dim avg As Double
Dim lg As String
End Structure
Dim student(MAXSIZE) As StudentData
'The Choose Input File event procedure should be enabled so as the user clicks this button first, because a text file should be chosen.
'This sub will collect the data in the input text file to the structure of the array and store them there in order to not waste IO.
Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click
lstOutput.Items.Clear() 'The list box should be cleared before choosing a file.
Dim count As Integer = 0 'Counter of the arrays and do-until loop.
Dim recFile As System.IO.StreamReader 'To begin openning the file.
'Set the filter of the OpenFileDialog control and specify the directory to be initially displayed.
ofdReadFile.Filter = "Text files (*.txt)|*.txt|All Files (*.*)|*.*"
ofdReadFile.InitialDirectory = "C:\Temp"
'If the user selected a file, the filename is displayed. An if-statement is used to control this. Then a do-until loop is used to
'collect the data until the end of the file is reached.
If (ofdReadFile.ShowDialog = DialogResult.OK) Then
recFile = System.IO.File.OpenText(ofdReadFile.FileName)
Do Until recFile.Peek = -1
Dim n As Double = 0 'n variable is for the for-loop to read the grades of each student.
Dim total As Double = 0 'total variable is to help calculate the average of each student.
student(count).name = recFile.ReadLine()
student(count).ssn = recFile.ReadLine()
student(count).numGrades = Val(recFile.ReadLine())
For n = 1 To student(count).numGrades
total += Val(recFile.ReadLine)
Next
total /= student(count).numGrades
student(count).avg = total
student(count).lg = LetterGrade(student(count).avg)
count += 1
Loop
recFile.Close() 'Close the file.
End If
End Sub
'The FullName function takes the name of the student in the format "Last, First" and returns the name in the format "First Last".
Function FullName(ByVal fn As String) As String
'The nested if-statements are to validate the array name and if there is a comma in the full name.
Dim comma, count As Integer
Dim first As String
Dim last As String
If fn.Length > 0 Then 'THIS IS WHERE THE DEBUGGING STOPS ME.
If fn.IndexOf(",") > 0 Then
comma = fn.IndexOf(",") 'The comma variable is given the index of the comma value using the IndexOf method.
first = fn.Substring(comma + 1).Trim
last = fn.Substring(0, comma)
End If
End If
Return first & " " & last 'Return the reformatted full name.
End Function
Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
Dim count As Integer 'Declare counter for the arrays.
'There will have to be a count loop here to show the list of names and info!!!
For count = 0 To (student.Length - 1)
lstOutput.Items.Add(String.Format("{0, -28}{1, -14}{2, -6}{3, -7}{4, -6}", FullName(StudentData), SSNum(student(count).ssn), _
student(count).numGrades, student(count).avg, LetterGrade(student(count).avg)))
End Sub
There is more to the code, but this is what is surrounding my problem. If you need more questions about the purpose of this project, ask!
Last edited by mizxtara; Mar 7th, 2007 at 09:45 PM.
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
|