Click to See Complete Forum and Search --> : Dynamic Arrays Homework & I need Help!


Chiba Terrell
February 15th, 2000, 08:53 PM
I have a problem with an Array & a module, Im not sure how to do this...
I have a form where Students can enter their name, & grades under columns Homework, the arrays created Average the scores
across for each student & down for each homework assignment.
Now the instructor has written: Note that cmdNewHomework_Click(), cmdNewStudent_Click(), and Form_Load() all contain the same
, long statement: ReDim Grades ( 0 To NumStudents - 1, 0 To NumHomeworks -1)
He wants us to Move this statement with calls to a new sub.
After doing this he wants us to have only one ReDim statement in the whole program, it should be executed when the form is loaded
into memory, and when the user clicks either the "New Student" or the "New Homework" command button.
Can anyone offer some suggestions on how to write this... I sure would appreciate..... Thanks

Clearcode
February 16th, 2000, 05:24 AM
Assuming numstudents and numhomeworks are form level variables then a sub in the form like:

private Sub ResizeArray()
Redim Preserve Grades(0 to NumStudents-1,0 to Numgrades-1)
End Sub



Then in the 3 bits of code....

private Sub Form_Load()
NumStudents = 1
NumGrades = 1
Call ResizeArray
End Sub



but for extra credit, have NumGrades and NumStudents as property let/get variables e.g.:

'Declarations section
Dim m_NumStudents as Integer
Dim m_NumGrades as Integer
private property let NumStudents(byval iNewSize as Integer)
If iNewSize > 0 then
m_NumStudents = iNewSize
Call ResizeArray()
End If
End property
private property get NumStudents() as Integer
NumStudents = i_NumStudents
End property
'..and the same for NumGrades
private Sub ResizeArray()
Redim Preserve Grades(0 to m_NumStudents-1,0 to m_Numgrades-1)
End Sub
'usage.....
private Sub Form_Load()
NumGrades = 1
NumStudents = 1
End Sub
private Sub AddStudent_Click()
NumStudents = NumStudents + 1
End Sub
private Sub AddGrade_Click()
NumGrades = NumGrades + 1
End Sub




Of course such an application should not be done with arrays at all but instead with Collection Classes...but I guess your teacher is getting round to that soon :-)