2 Replies - 1913 Views - Last Post: 09 December 2012 - 12:01 PM Rate Topic: -----

#1 compwhiz345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-December 12

Use a MessageBox to a list of item from Count List Box

Posted 07 December 2012 - 01:16 PM

Hi everyone,

I have been working on this for several hours, and I still cannot figure it out. I'm close but the Items are not adding correctly. What this form is suppose to do is by clicking on this button will display a message box that shows the sum of the capacity values of all airplanes in the List(of Product) as well as the count of how many airplanes are in the list. Create these totals by looping through the list, and checking for the type of each item in the list. If an item is an Airplane instance:

Increment the airplane count.
Cast the Product instance as an Airplane so that it can be referred to as an Airplane (and not just more generically, as a Product instance)
Once this is done, then you can access the Capacity property that will be used in summing up all capacity values.

The code will have to go through all items in the list. All Cars will be ignored, and only if an item is an Airplane does the code update the count and capacity total variables.

Option Strict On
Option Explicit On
Option Infer Off

Public Class frmProductMaint

    Dim products As New List(Of Product)

    Private Sub frmProductMaint_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
        products.Add(New Car("1000", "VW", "Purple", 10000))
        products.Add(New Airplane("2000", "Lear Jet", CStr(12), 500000))
        products.Add(New Airplane("3000", "Piper Cub", CStr(4), 75000))
        products.Add(New Car("4000", "Cadillac", "Pink", 40000))
        Me.FillProductListBox()
    End Sub

    Private Sub FillProductListBox()
        lstProducts.Items.Clear()
        For Each p As Product In products
            lstProducts.Items.Add(p.GetDisplayText(vbTab))
        Next
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object,
                ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim newProductForm As New frmNewProduct
        Dim product As Product = newProductForm.GetNewProduct
        If product IsNot Nothing Then
            products.Add(product)
            Me.FillProductListBox()
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object,
                ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim i As Integer = lstProducts.SelectedIndex
        If i <> -1 Then
            Dim product As Product = products(i)
            Dim message As String = "Are you sure you want to delete " _
                & product.Description & "?"
            Dim button As DialogResult = MessageBox.Show(message, _
                "Confirm Delete", MessageBoxButtons.YesNo)
            If button = DialogResult.Yes Then
                products.Remove(product)
                Me.FillProductListBox()
            End If
        End If
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnShowTotalAirplaneCapacity_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowTotalAirplaneCapacity.Click
        For Each p As Product In products
            Dim Airplane1 As Airplane
            Dim a As String = p.GetType.Name
            If a = "Airplane" =  Then
                Airplane1 = CType(p, Airplane)
                Airplane1.Capacity = CStr(MessageBox.Show("Total Capacity: " & (CStr(lstProducts.Items.Count) & vbCrLf &
                                                          "Total Number of Planes: " & (CStr(lstProducts.Items.Count)))))
            End If
        Next
    End Sub
End Class



This portion of the code it what I am having the most difficult time with:

Private Sub btnShowTotalAirplaneCapacity_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowTotalAirplaneCapacity.Click
        For Each p As Product In products
            Dim Airplane1 As Airplane
            Dim a As String = p.GetType.Name
            If a = "Airplane" =  Then
                Airplane1 = CType(p, Airplane)
                Airplane1.Capacity = CStr(MessageBox.Show("Total Capacity: " & (CStr(lstProducts.Items.Count) & vbCrLf &
                                                          "Total Number of Planes: " & (CStr(lstProducts.Items.Count)))))
            End If
        Next
    End Sub
End Class


I have also attached a zip file of the homework. The portion that I am confused about is in the FrmProductMaint, under btnShowTotalAirplaneCapacity_Click.

Any help would be appreciated.

Thanks,

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Use a MessageBox to a list of item from Count List Box

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,868
  • Joined: 02-June 10

Re: Use a MessageBox to a list of item from Count List Box

Posted 08 December 2012 - 09:18 AM

Your assignment is to do this in VB6? You're deliberately going to school to learn a language that's been dead for a decade? Should this be moved to VB.NET?

Please read: This is not the VB.NET forum
Please read: Is learning VB6 now a complete waste of time?

compwhiz345: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated DEC 2012
Spoiler




Quote

05            If a = "Airplane" =  Then



Does this even compile and run? If it does then what is it really saying/doing?

Code spoken aloud said:

if a isEqualTo "Airplane" which is also equal to {no specified value} then do the following


Assuming this could even compile it would always be false: "Airplan" would never be equal to {no specified value}
Was This Post Helpful? 0
  • +
  • -

#3 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Use a MessageBox to a list of item from Count List Box

Posted 09 December 2012 - 12:01 PM

this is VB.NET, so someone should move this post there.


Airplane1.Capacity = CStr(MessageBox.Show("Total Capacity: " & (CStr(lstProducts.Items.Count) & vbCrLf & "Total Number of Planes: " & (CStr(lstProducts.Items.Count)))))



This is messed up. Bad. As I understand, you want a sum of air plains' capacities and a count. I think Capacity property is a numeric data type, so a cast from DialogResult(the result of MessageBox.Show) to Capacity cannot be done. Also you cannot assign a string to numeric data type without a cast.
I would do 2 things:
1. easy one : define two variables : airplainsCount and capacitySum before the for each part. then in your if remove the line and add
airplainsCount += 1
capacitySum += Airplain1.Capacity


2. harder one, but more elegant: Linq. Put this one on your "further research" list, you will find it very useful.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1