Results 1 to 19 of 19

Thread: A class inside a class?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    Wink A class inside a class?

    Hi there

    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
    Thanks for reading my post!

  2. #2
    AIS_DK
    Guest
    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.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    Angry Please help me debug my code...

    Hi,

    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
    Thanks for reading my post!

  4. #4
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Smile Code...

    Add the following code to the CStudent.cls:
    VB Code:
    1. Private Sub Class_Initialize()
    2.     Set mcCourseList = New CCourse
    3. End Sub
    4.  
    5. Private Sub Class_Terminate()
    6.     Set mcCourseList = Nothing
    7. End Sub

    It should work. Try it. If not, post back.
    Laterz
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  5. #5

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    It didn't work..

    Hi there,

    It didn't work Any ideas? Thanks for the suggestion!
    Thanks for reading my post!

  6. #6

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    Files

    Hi there,

    Just in case you are interested, I put my form and 2 class files here. Maybe it will be easier to troubleshoot. Thanks again!
    Attached Files Attached Files
    Thanks for reading my post!

  7. #7
    Addicted Member
    Join Date
    May 2000
    Location
    Mexico City
    Posts
    242
    Your problem is that you aren't using the Set keyword to assign object variables.

    Change this:
    VB Code:
    1. Public Property Let CourseList(pcCourseList As CCourse)
    2.     Set mcCourseList = New CCourse
    3.     mcCourseList = pcCourseList
    4. End Property
    To this:
    VB Code:
    1. Public Property Set CourseList(pcCourseList As CCourse)
    2.     Set mcCourseList = pcCourseList
    3. End Property

  8. #8

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    WoW!

    Wow, thanks so much gxpark! That works!

    Is that how you normally do it for a class property inside a class?

    Thanks again!
    Thanks for reading my post!

  9. #9
    Addicted Member
    Join Date
    May 2000
    Location
    Mexico City
    Posts
    242
    Not only for class properties, but for any other kind of object.

    I'm glad that helped you

  10. #10

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    GPA calculation

    Hi gxpark,

    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:
    1. Private Sub CommandButton1_Click()
    2.     Set classCourse = New CCourse
    3.     Set classStudent = New CStudent
    4.     classStudent.Number = TextBox1.Text
    5.     classStudent.Name = TextBox2.Text
    6.     classCourse.CourseNum = TextBox3.Text
    7.     classCourse.Credit = TextBox4.Text
    8.     classCourse.Grade = TextBox5.Text
    9.     classStudent.CourseList = classCourse
    10.     MsgBox classStudent.GPA
    11. End Sub

    GPA is a read-only property of the CStudent.cls. Here is how I defiend it on the CStudent.cls

    VB Code:
    1. Public Property Get GPA() As Integer
    2.        GPA = CourseList.Credit * CourseList.Grade  'where CourseList is a property of cStudent.cls as well, and is a type of CCourse class.
    3. End Property
    Thanks for reading my post!

  11. #11
    Addicted Member
    Join Date
    May 2000
    Location
    Mexico City
    Posts
    242
    Can you further explain the problem?

    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.

  12. #12

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    Talking Thanks!

    Thanks gxpark! You really know your stuff on classes! That's what I needed!
    Thanks for reading my post!

  13. #13

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    How to retain variables' values

    Hi gxpark,

    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?

    Thanks in advance!

    VB Code:
    1. Public Property Get GPA() As Integer
    2. Static totalgrade As Integer
    3. Static numberofcredits As Integer
    4.     totalgrade = totalgrade + (mcCourseList.Credit * mcCourseList.Grade)
    5.     numberofcredits = numberofcredits + mcCourseList.Credit
    6.     GPA = totalgrade / numberofcredits
    7. End Property
    Thanks for reading my post!

  14. #14
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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...

  15. #15

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    Still hopeful

    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!
    Attached Files Attached Files
    Thanks for reading my post!

  16. #16
    Addicted Member
    Join Date
    May 2000
    Location
    Mexico City
    Posts
    242

    Talking Eureka!

    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.

  17. #17
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Or create a collection object, where are your student object will be held.

  18. #18

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    37

    Still couldn't quite get it

    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.

    Thanks!
    Attached Files Attached Files
    Thanks for reading my post!

  19. #19
    Addicted Member
    Join Date
    May 2000
    Location
    Mexico City
    Posts
    242
    The GPA isn't calculated correctly because you're using mcCourseList.Credit where you should be using mcCourseList.Mark:
    VB Code:
    1. Public Property Get GPA() As Integer
    2. Static totalgrade As Integer
    3. Static numberofcredits As Integer
    4.     totalgrade = totalgrade + (mcCourseList.Credit * mcCourseList.Grade)
    5.     numberofcredits = numberofcredits + mcCourseList.Mark
    6.     GPA = totalgrade / numberofcredits
    7. End Property
    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).
    Attached Files Attached Files
    Live your own life, for you will die your own death.

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