Can someone please tell me if it's possible to have a class inside a class? ie. I have 2 classes, one is called CStudent, and the other class is called CCourseGrade.
CStudent has these properties: StudNumber, StudName, GPA.
CCourseGrade has these properties: CourseNumber, CourseName, Credits, and Mark
For my CStudent class, can I also include CCourseGrade as one of the properties in the CStudent class?
If so, how do I do this properly?
Would it be right to do this in my CStudent class?
Private mcCourseList As CCourseGrade
Public Property Let CourseList(pcCourseList As CCourseGrade)
mcCourseList = pcCourseList
End Property
Public Property Get CourseList() As CCourseGrade
CourseList = mcCourseList
End Property
That's the way to do it, I would however remove the let property, since you don't wan't any outsiders to change the class reference. Let the student class take care of the initialisation the grade class, and let every other class access it through the student class.
Can someone please show me how to do this properly? I have 2 classes, one called CStudent, the other is called CCourse. Below is the main form. I am trying to put classCourse to classStudent.CourseList but it's failing... (last line of the main form). It might be easier to put the whole code to run in VB... Thanks for any help you will be able to suggest.
CStudent Class Code
Private mstrNumber As String
Private mstrName As String
Private mintGPA As Integer
Private mcCourseList As CCourse
Public Property Let Number(pstrNumber As String)
mstrNumber = pstrNumber
End Property
Public Property Get Number() As String
Number = mstrNumber
End Property
Public Property Let Name(pstrName As String)
mstrName = pstrName
End Property
Public Property Get Name() As String
Name = mstrName
End Property
Public Property Get GPA() As Integer
Dim clsCcourse As CCourse
GPA = clsCcourse.Credit * clsCcourse.Grade
End Property
Public Property Let CourseList(pcCourseList As CCourse)
Set mcCourseList = New CCourse
mcCourseList = pcCourseList
End Property
Public Property Get CourseList() As CCourse
CourseList = mcCourseList
End Property
CCourse Class Code
Private mstrCourseNum As String
Private mintGrade As Integer
Private mintCredit As Integer
Public Property Let CourseNum(pstrCourseNum As String)
mstrCourseNum = pstrCourseNum
End Property
Public Property Get CourseNum() As String
CourseNum = mstrCourseNum
End Property
Public Property Let Grade(pintGrade As Integer)
mintGrade = pintGrade
End Property
Public Property Get Grade() As Integer
Grade = mintGrade
End Property
Public Property Let Credit(pintCredit As Integer)
mintCredit = pintCredit
End Property
Public Property Get Credit() As Integer
Credit = mintCredit
End Property
Main Form
Dim classCourse As CCourse
Dim classStudent As CStudent
Private Sub CommandButton1_Click()
Set classCourse = New CCourse
Set classStudent = New CStudent
classStudent.Number = TextBox1.Text
classStudent.Name = TextBox2.Text
classCourse.CourseNum = TextBox3.Text
classStudent.CourseList = classCourse
End Sub
I hope you can help me out with this one... I'm stucked on calculating the GPA. Somehow, the concept of classes is still quite alien to me. Hope you can help me out.
I added a messagebox to print the GPA of the student.
VB Code:
Private Sub CommandButton1_Click()
Set classCourse = New CCourse
Set classStudent = New CStudent
classStudent.Number = TextBox1.Text
classStudent.Name = TextBox2.Text
classCourse.CourseNum = TextBox3.Text
classCourse.Credit = TextBox4.Text
classCourse.Grade = TextBox5.Text
classStudent.CourseList = classCourse
MsgBox classStudent.GPA
End Sub
GPA is a read-only property of the CStudent.cls. Here is how I defiend it on the CStudent.cls
VB Code:
Public Property Get GPA() As Integer
GPA = CourseList.Credit * CourseList.Grade 'where CourseList is a property of cStudent.cls as well, and is a type of CCourse class.
Although I don't see any error there, as a general rule, you should use the private object instead of the "Get" property when the only thing that property does is pass the object (i.e., doesn't validate any data, modify the object or any other stuff).
So, instead of CourseList, use mcCourseList.
Live your own life, for you will die your own death.
Sorry to be bugging you again, but for the life of me I still can't get my GPA calculation to work properly.
GPA calculation is calculated as total(credit*grade)/# of credits.
Example as follows:
Course Grade Credits
1st Course - 100 * 3 = 300
2nd Course- 98 * 3 = 294
----------
594/6 = 99
where 99 is the GPA grade.
What happens to me is that I'm having problems trying to retain the values of my totalgrade and numberofcredits variables. After hitting on the cmdOk button, these values revert back to 0, so it loses track of old values... What am I doing wrong?
Another suggestion is that you could make your grade class a 'Public Not Creatable' object. Meaning, that you can not directly instanciate an object from this class, rather go throught it's parent class. For example, the only way to access this class is to instantiate an object from the CStudent class. Just something to ponder over...
I'm still hopeful that somebody can show me how this is supposed to work... I've included what I have so far... my only problem now is how to get the GPA to successfully calculate. I've enclosed my files in a zip file in case anybody wants to help. Thanks again!
I see what the problem is... You are creating new objects every time you add a course, thus efectively deleting all previous data.
So, you need to preserve one CStudent object for all the session, i.e. create it on the Load event of the main form. AND don't create a CCourseGrade object, access this info through Form1.clsCStundent.CourseList...
I hope this clears everything up!
Live your own life, for you will die your own death.
gxpark, thanks for your input. I understand what you meant by creating the new student class everytime
However, i found out i was doing this the hard way, you know, lethal is right, why not just use a collection object, which will be easier. I tried this for learning purposes, and I find this is easier. However, still couldn't get GPA to work successfully. I'm not sure how to write the Property Get for my GPA (it should be a read only property). I'm really sorry to bug you guys again on this, but for a novice like me, all this stuff is really difficult to understand (especially if your teacher doesn't seem to be teaching it properly )
Anyways, if you still want to help me, I zipped up my code again (for the nth time), this time I'm using collection instead of class inside a class.
Anyway, I attached a streamlined version of your project (I didn't realized what the error was until I finished "re-coding it" ) so you can study a few techniques I used... Feel free to ask if you don't understand how something works (or why).
Live your own life, for you will die your own death.