Results 1 to 9 of 9

Thread: Classes in Classes

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Classes in Classes

    In my teacher gradebook program, I have several VB classes set up for the framework of the program.

    I have the following classes so far.

    Student (has all the student info like names, address, booknumber, grade list, attendance list, parent info etc)
    Parent (info about a student's parents
    Assignment (info about each assignment like page, perfect score, date assigned, kind of assignment - test or homework, etc)
    Grade (has fields Value and Info, and a function PercentCorrect()

    In the student class, I've set up a 2-dimensional array of grades (of type Grade Class).

    But I'm not sure how to set up Get and Set statements within the Student class that refer to the Grade class.

    In my main program, I want to do something like
    Student(i).Grade(1,1).Value = "85" 'yes I know it's a string
    Student(i).Grade(1,1).Info = "needs to redo this one" 'field for my notes on the student
    Student(i).Grade(1,1).PercentCorrect = ????

    Now another point. The perfect score for this assignment is held in the Assignment class. I can access it with something like
    ps = Assignment(1,1).PerfectScore
    Now I haven't tried it yet but will I be able to access this PerfectScore info from my Grade class to actually calculate the perfect score? Or do I need to include PerfectScore as a field in the Grade class? If so, then I have to update it for EACH student and for EACH grade. If I keep it in the Assignment class, then it only has to be changed once.

    Confused yet? I am.
    Any tips for me?

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Classes in Classes

    why is it a 2d array?

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Classes in Classes

    If you include the assignment for the student as a property, you should be able to access the PerfectScore for that assignment. For each assignment, that is. But your 2-D array logic escapes me.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Classes in Classes

    Whenever you think you need a 2D array, consider whether or not you could make a 1D array of some type of class instead. 1D arrays are vastly easier to work with in VB, especially since the List (of Type) generic list was added in 2005.
    My usual boring signature: Nothing

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Classes in Classes

    I'd be inclined to use a Dictionary for this. Each Student takes Assignments and receives a Grade for each one. There needs to be a specific relationship between the Assignments and the Grades and the Dictionary provides that:
    vb.net Code:
    1. Public Class Student
    2.  
    3.     Private _assignments As New Dictionary(Of Assignment, Grade)
    4.  
    5.     Public ReadOnly Property Assignments() As Dictionary(Of Assignment, Grade)
    6.         Get
    7.             Return Me._assignments
    8.         End Get
    9.     End Property
    10.  
    11. End Class
    Now, when you create an Assignment you can add it to all the Students who take it:
    vb.net Code:
    1. Dim assignment1 As New Assignment
    2.  
    3. For Each s As Student In students
    4.     s.Assignments.Add(assignment1, Nothing)
    5. Next s
    When a student completes an assignment you can set the grade for it:
    vb.net Code:
    1. someStudent.Assignments(assignment1) = someGrade
    If you want to find out what grade a student got for a particular assignment you can get it easily:
    vb.net Code:
    1. Dim g As Grade = someStudent.Assignments(assignment1)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Classes in Classes

    But Grade is a VB Class that has several properties.

    vb Code:
    1. Public Class Grade
    2.     Private pValue as string
    3.     Private pInfo as string
    4.  
    5.     Public Property Value as string
    6.     Public Property Info as string
    7.     Public Function PercentCorrect as Integer
    8. End Class

    So I don't see how
    Dim g As Grade = someStudent.Assignments(assignment1)
    will return the Value or Info or even PercentCorrect for me.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Classes in Classes

    kleinma, it's a 2d array because I have 4 sets of grades for each student. Our school year is divided in to 4 "9 weeks" periods. I need grades for the "1st 9 weeks", "2nd 9 weeks", "3rd 9 weeks", and "4th 9 weeks".

    So a 2d array makes sense to me. Don't you agree? Am I missing something?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Classes in Classes

    If I could figure out a way to make the DataGridView control keep up with all the extra "assignment info" then I wouldn't need to keep up with the array at all.

    I couldn't probably create a custom GridControl and add extra properties to the Grid's Columns (assignments) and Rows (students).

    For example, each DataGridViewColumn (grades/assignments) could have the following extra properties
    .Page
    .PerfectScore
    .DateAssigned
    .DateTurnedIn
    .PerfectScore
    etc.

    And each DataGridViewRow (students) could have
    .FirstName
    .LastName
    .MinimumDGrade (some students have modified grade scales)
    .MinimumCGrade
    .MinimumBGrade
    .MinimumAGrade
    .EmailAdress
    .HomePhone
    .BookNumber
    .BookTurnedIn
    etc.

    Then, when the cursor moves to a different cell in the grid, the .CellChanged event could fire code to update the displayed information. There would be no need for any trick array manipulation because all of the info would be stored in the controls.

    I had wanted to do it this way last year when I started rewriting my old GradeBook which I initially wrote in VB6. It still does the job but I'd like to have some new features - like attendance.

    Does anyone think I should go with the non-array idea?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Classes in Classes

    Quote Originally Posted by DroopyPawn
    But Grade is a VB Class that has several properties.

    vb Code:
    1. Public Class Grade
    2.     Private pValue as string
    3.     Private pInfo as string
    4.  
    5.     Public Property Value as string
    6.     Public Property Info as string
    7.     Public Function PercentCorrect as Integer
    8. End Class

    So I don't see how
    Dim g As Grade = someStudent.Assignments(assignment1)
    will return the Value or Info or even PercentCorrect for me.
    You don't see how? You just said it yourself. Grade is a class that has several properties. This:
    VB.NET Code:
    1. Dim g As Grade = someStudent.Assignments(assignment1)
    gives you a Grade object, from which you can get any or all of those properties:
    VB.NET Code:
    1. Dim value As String = g.Value
    2. Dim info As String = g.Info
    3. Dim percentCorrect As Integer = g.PercentCorrect
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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