CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2000
    Posts
    1

    Dynamic Arrays Homework & I need Help!

    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


  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Dynamic Arrays Homework & I need Help!

    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 :-)

    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured