Visual Basic 2008 Inheritance Question

airbornflghtairbornflght Houston, TX Icrontian
edited December 2009 in Science & Tech
Ok. I'm trying to wrap up my final for a programming class I'm taking and I've completed everything but this one bug I can't get worked out.

I have 4 classes defined. The first is called person. And there are three others (elementary, intermediate, highschool) that inherit from the base class.

I have everything working, or so I thought. The goal is to create different classes of objects based on one the user selects as the patron type. Here is the code creating the object and placing it in an arrayList:
Private Sub PointsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PointsToolStripMenuItem.Click

        Try
            bookCount = Integer.Parse(tbBooksRead.Text)
        Catch ex As Exception
            MessageBox.Show("Number of books must be an integer", "Error", MessageBoxButtons.OK)
            Exit Sub
        End Try

        'Polymorhpism for the win.
        If rbElementary.Checked Then
            patron = New elementary(tbName.Text, bookCount)
        ElseIf rbIntermediate.Checked Then
            patron = New intermediate(tbName.Text, bookCount)
        ElseIf rbHighschool.Checked Then
            patron = New highSchool(tbName.Text, bookCount)
        Else
            MessageBox.Show("Please select which type of student this is.", "Error", MessageBoxButtons.OK)
            Exit Sub
        End If

        readingClubMembers.Add(patron)
        tbPoints.Text = patron.pointsEarned()

    End Sub

That is all well and good. At least I hope. The tricky bit is I have another form that is supposed to show a summary of all the patrons detailed by which class they are. But something is going wrong because all of the entries are showing up but they all show the same values. Here is the code from the summary form:
Private Sub p129_summary_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If p129.readingClubMembers.Count = 0 Then
            MessageBox.Show("No reading club members to show.", "No members", MessageBoxButtons.OK)
            Me.Close()
        Else
            Label1.Text = "Name:" & vbNewLine & vbNewLine
            Label4.Text = "Name:" & vbNewLine & vbNewLine
            Label7.Text = "Name:" & vbNewLine & vbNewLine

            Label2.Text = "Books Read:" & vbNewLine & vbNewLine
            Label5.Text = "Books Read:" & vbNewLine & vbNewLine
            Label8.Text = "Books Read:" & vbNewLine & vbNewLine

            Label3.Text = "Points Earned:" & vbNewLine & vbNewLine
            Label6.Text = "Points Earned:" & vbNewLine & vbNewLine
            Label9.Text = "Points Earned:" & vbNewLine & vbNewLine

            For i As Integer = 0 To p129.readingClubMembers.Count - 1
                If TypeOf p129.readingClubMembers(i) Is elementary Then
                    Label1.Text += p129.readingClubMembers(i).name & vbNewLine
                    Label2.Text += p129.readingClubMembers(i).books & vbNewLine
                    Label3.Text += p129.readingClubMembers(i).pointsEarned & vbNewLine
                ElseIf TypeOf p129.readingClubMembers(i) Is intermediate Then
                    Label4.Text += p129.readingClubMembers(i).name & vbNewLine
                    Label5.Text += p129.readingClubMembers(i).books & vbNewLine
                    Label6.Text += p129.readingClubMembers(i).pointsEarned & vbNewLine
                ElseIf TypeOf p129.readingClubMembers(i) Is highSchool Then
                    Label7.Text += p129.readingClubMembers(i).name & vbNewLine
                    Label8.Text += p129.readingClubMembers(i).books & vbNewLine
                    Label9.Text += p129.readingClubMembers(i).pointsEarned & vbNewLine
                Else
                    MessageBox.Show("Object type: " & p129.readingClubMembers(i).GetType.ToString & " is not allowed.", "Error", MessageBoxButtons.OK)
                End If
            Next
        End If
    End Sub

I've googled and looking through my book but I am coming up empty. I even tried different ways to attack the problem and kept ending up with the same problem. And advice?

Just to be clear, the objects are all being sorted correctly. It's just that each object has the same values as every other one. I thought that by using the 'new' keyword it would create a new instance of it each time. Obviously I'm mistaken. What do I need to do to fix my problem?

Comments

  • edited December 2009
    *Edit*. NVM, i'm rethinking...
  • pragtasticpragtastic Alexandria, VA Icrontian
    edited December 2009
    Couldn't hurt to see your person and inheriting classes to get a better picture of the situation.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited December 2009
    Public Class elementary
        Inherits person
        Sub New(ByVal inputName As String, ByVal numRead As Integer)
            PersonName = inputName
            booksRead = numRead
        End Sub
    End Class
    
    Public Class intermediate
        Inherits person
        Sub New(ByVal inputName As String, ByVal numRead As Integer)
            PersonName = inputName
            booksRead = numRead
        End Sub
    End Class
    
    Public Class highSchool
        Inherits person
        Sub New(ByVal inputName As String, ByVal numRead As Integer)
            PersonName = inputName
            booksRead = numRead
        End Sub
    End Class
    
    
    Public Class person
        Friend Shared booksRead As Integer
        Friend Shared PersonName As String
        Private Shared points As Integer
    
        Sub New()
        End Sub
    
        Sub New(ByVal inputName As String, ByVal numRead As Integer)
            PersonName = inputName
            booksRead = numRead
        End Sub
    
        Public Property name() As String
            Get
                Return PersonName
            End Get
            Private Set(ByVal value As String)
                PersonName = value
            End Set
        End Property
    
        Public Property books() As Integer
            Get
                Return booksRead
            End Get
            Private Set(ByVal value As Integer)
                If value >= 0 Then
                    booksRead = value
                Else
                    MessageBox.Show("Number of books read must be positive.", "Error", MessageBoxButtons.OK)
                End If
    
            End Set
        End Property
    
        Public ReadOnly Property pointsEarned() As Integer
            Get
                points = 0
                Dim bookCount = booksRead
                Do Until bookCount = 0
                    If bookCount > 6 Then
                        points += 20
                    ElseIf bookCount > 3 Then
                        points += 15
                    Else
                        points += 10
                    End If
                    bookCount -= 1
                Loop
                Return points
            End Get
        End Property
    End Class
    
  • _k_k P-Town, Texas Icrontian
    edited December 2009
    commentsinyourcodehelp. So your issue is your instantiation of a new object. I assume that when you add new people, under a new name, they still come up with the same name and score no matter what you do. The only thing that is changing is the School attribute, since items are being sorted into their correct division.

    Quick guess. The top set of code has you giving information on School level to the patron attribute and that is the only thing that is passed to the readingClubMembers.add(). I am assuming that the section about the patron object building in the first set runs every time someone clicks on stuff. To me I would have it so that there is a submit button or function that reads all the information from the GUI I pane and then creates the patron and pushes it onto a stack/array/set. I thought I saw where you were adding items to a set but can't find it.
  • pragtasticpragtastic Alexandria, VA Icrontian
    edited December 2009
    To reiterate what _k_ said, you want to make sure that the input from your GUI is what you expect for each Person object before it gets stored into whatever collection you are using.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited December 2009
    Well what happens is in the file menu there is a button to click and that is the top set subroutine.

    It takes the info from the text boxes and creates a new object based on the radio button that is selected. After that object is instantiated it calls for the "points" property to be returned into the read only textbox. Then it is added to the arrayList.

    I don't understand how I am creating a new object each time, yet they are taking each others property values. And I'm pretty sure I'm doing the polymorphism bit right. By declaring a patron up top and then instantiating it as one of the three daughter types.

    It's all terribly confusing to me why it's not working as expected. And sorry about the lack of comments.
  • _k_k P-Town, Texas Icrontian
    edited December 2009
    Its terribly confusing right now but maybe you can change that. See if you can modify your ouput so that it displays when the arrayList is first navigated and when its done being navigated, display a count of items in the arrayList, and also put some kind of display in your poly section(int = -1 and have it change to 1,2, or 3 depending on the school). The last section you could simply tack the value on the end of their name or create an extra variable that gets loaded into the patron object.

    This is so you are sure you know when the array is being used and not, how many objects are loaded into them, and that your poly code is being navigated logically. You have a multi class program that uses poly and methods are passed multi-variable arguments so there needs to be display checks set to as many actions as possible, or just the main ones, when you are looking for a problem. Objects not being instantiated correctly can be really tricky any short of seeing an obvious blunder this is the next best way.
Sign In or Register to comment.