Reputation: 579
If you can use method chaining to get to a results such as the line below
cReportcard.student("doe").Metric("math").score
How do you take a property back down the chain?
I have a collection of custom classes for subjects (e.g., math, language arts, pe...). The custom class has a student,score, teacher, classroom, period.
How can I
cReportcard.student("doe").Metric("math").score=0.96
and have the value chain backward to the originating class collection?
I imagine this to be something like the "parent" property of the Worksheet class. I just cannot figure out how to do it.
So far I have designed the a table scheme with 4 tables (Student, Metric, Grades, Periods)
Student(ID [pk], NAME, DOB)
Metric (ID [pk], NAME)
Period (ID [pk], METRIC_ID [fk], GRADE_LEVEL, START_TIME, LENGTH, CLASS_RM, TEACHER_ID [fk])
Grade (ID [pk], PERIOD_END_DATE, PERIOD_ID [fk], STUDENT_ID [fk], METRIC_SCORE
)
Upvotes: 0
Views: 213
Reputation: 6120
I think you might want to put a little more time into defining your exact use cases to make sure those tables will quickly allow you to perform any operations you will need. But assuming that's the final structure, I would design the classes like this:
clsGrade:
'It would be better to have them all private and provide get/set properties
Private ID As Long
Public Student As clsStudent
Public Period As clsPeriod
Public Period_End_Date As Date
Public Metric_Score As Long
'This assumes that, per your example, the only fields necessary to find a "grade" are student name and metric
Public Sub Load(studentName As String, Metric As String)
Dim rs As Recordset
'SQL query to get the grade for a particular student/metric
Set Student = New clsStudent
Student.Load rs!STUDENT_ID
Set Period = New clsPeriod
Period.Load rs!PERIOD_ID
End Sub
Public Sub Save()
'SQL query to save the grade
'You would have to decide whether or not this cascades and saves the Student/Period/Metric info as well.
Student.Save
Period.Save
End Sub
clsMetric:
'It would be better to have them all private and provide get/set properties
Private ID As Long
Public Name As String
Public Sub Load(ID As Long)
'SQL to load a metric by ID
End Sub
Public Sub Save()
'SQL to save the metric
End Sub
clsPeriod:
'It would be better to have them all private and provide get/set properties
Private ID As Long
Public Metric As clsMetric
Public Grade_Level As Long
Public Start_Time As Date
Public Length As Date
Public Class_Rm As String
Public Teacher_ID As Long
Public Sub Load(ID As Long)
Dim rs As Recordset
'SQL to load a period by ID
'Also load the child metric
Set Metric = New clsMetric
Metric.Load rs!METRIC_ID
End Sub
Public Sub Save()
'SQL to save the period
'Also save the child metric
Metric.Save
End Sub
clsStudent:
'It would be better to have them all private and provide get/set properties
Private ID As Long
Public Name As String
Public DOB As Date
Public Sub Load(ID As Long)
'SQL to load a student by ID
End Sub
Public Sub Save()
'SQL to save the student
End Sub
And then you could use them like so:
Public Sub Main()
Dim grade As clsGrade
Set grade = New clsGrade
grade.Load "doe", "math"
grade.Metric_Score = 0.96
grade.Save
End Sub
If you want to streamline it and get fancy, you can do the Get/Let Properties and add a "dirty" bit to each class to keep track of whether anything has been changed since it was loaded or not. That way you don't save information which hasn't changed.
Upvotes: 2