Visual Basic 2008 Inheritance Question
airbornflght
Houston, TX Icrontian
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:
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:
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?
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?
0
Comments
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.
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.
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.