locked
Code For Calculating Total Price RRS feed

  • Question

  • Hi

    I've been trying to learn how to develop a windows form application that users can order a variety of phones from just in order for myself to learn for a school assignment. I've wrote a list of three made up phones all at different prices named, The basic blue ($89.50), intermediate Intel ($170), and the gold browser ($317.99) while also providing three different plans being bronze($29), silver($49), and gold($69). I wanted to make it so when the user selects the phone they want from the ListBox and their selected plan, the total cost will be listed at the bottom of the screen in a read only text box. I think I need to use a public class for this to work which I attempted to do, as you'll see in the code below however i'm not sure what to do after that. This is my first time using classes so if someone could help me out it would be greatly appreciated. I don't necessarily need the code to copy. It would just be helpful if someone could explain and guide me through what I need to do to achieve this.  

    Here's what I've done so far

    Public Class Phones
        Public Basic_Blue As String
        Public Intermediate_Intel As String
        Public Gold_Browser As String
        Public No_Phone As String
        Public Price As PhonePrice
    
        Public Enum PhonePrice
            Phone1
            Phone2
            Phone3
            NoPhone
        End Enum
        Public Sub New(BB As String, II As String, GB As String, P As Integer, NP As String)
            Basic_Blue = BB
            Intermediate_Intel = II
            Gold_Browser = GB
            Price = P
            No_Phone = NP
    
            Select Case P
                Case 89.5
                    BB = PhonePrice.Phone1
                Case 170
                    II = PhonePrice.Phone2
                Case 317.99
                    GB = PhonePrice.Phone3
                Case 0
                    NP = PhonePrice.NoPhone
            End Select
        End Sub
    End Class


    Wednesday, December 21, 2016 6:40 AM

Answers

  • William,

    This is a good opportunity to set these up using inheritance. If you think about it, you'll have two classes which will have two members each: The name (of the phone or of the plan) and the price.

    Give the following a look. Hopefully you can see how this first one works (the phones) and with that, set up the other one (the plan):

    Option Strict On Option Explicit On Option Infer Off Public MustInherit Class NameAndPrice Protected _name As String Protected _sellPrice As Decimal Public Sub New(ByVal name As String, _ ByVal price As Decimal) _name = name.Trim _sellPrice = price ' This has a basic fallacy: What if the name is ' null or what if the price is zero (or worse yet, ' negative)? ' ' This area is where it can be handled but for the ' sake of simplicity I’m not doing that here. ' ' Just do know that a ‘real world’ situation would ' need to check for things like this. ' ' Also, there is no test for duplicate entries – ' something else that you’d likely want to do. End Sub ''' <summary> ''' Gets the name associated with this entry. ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property Name As String Get Return _name End Get End Property ''' <summary> ''' Gets the selling price associated with this entry. ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property SellPrice As Decimal Get Return _sellPrice End Get End Property End Class Public Class Phones Inherits NameAndPrice Private Sub New(ByVal name As String, ByVal price As Decimal) MyBase.New(name, price) End Sub Public Shared Sub _ AddNew(ByRef phoneList As List(Of Phones), _ ByVal name As String, _ ByVal sellPrice As Decimal) ' As before, validation should be done here: ' ' What if the phone list (a generic List(Of ' Phone)) is null? ' ' The other two won't need validation here if the ' constructor of the base (abstract) class were ' done correctly. The reason being that otherwise ' it's a duplication of effort. phoneList.Add(New Phones(name, sellPrice)) ' In the one line above, it will take the list ' that you hand it and will add to it an instance ' of the "Phones" class. ' ' You - as the consumer of the class - can't do ' that directly because the constructor is ' private; only a method of the class can directly ' access it. ' ' In the code of your form, you might want to ' experiment with it though - just to show that ' you can't create a new instance of this class ' from there. ' ' In time, when dealing with classes, you'll learn ' about "patterns" and this is a variation of a ' very common pattern known as the "factory ' pattern". End Sub Public Shared Function _ GetInstance(ByVal phoneList As List(Of Phones), _ ByVal name As String) As Phones Dim retVal As Phones = Nothing If phoneList IsNot Nothing AndAlso _ Not String.IsNullOrWhiteSpace(name) Then ' I would normally use LINQ here, but there's ' nothing wrong with simple iteration: For Each p As Phones In phoneList If p.Name = name Then retVal = p Exit For ' Look at this carefully. Do you see what's going ' on here? ' ' It's iterating through every instance of ' "Phones" that's in the list that you give it. ' ' If the member (the property) named "Name" ' matches, it will then set my private variable ' called "retVal" to that instance and, following ' that will then exit the loop. End If Next End If Return retVal End Function Public Shared Function _ GetPhoneSellingPrice(ByVal phoneList As List(Of Phones), _ ByVal name As String) As Decimal ' In all likelihood you’ll be using this function ' more than the ‘GetInstance’ method. ' ' After all, this class only has two members and ' you’re using one of them to find the other, so ' this method makes more sense to use! ;-) ' ' Still though, I’ll make use of the ‘GetInstance’ ' method as shown below: Dim retVal As Decimal = 0 Dim p As Phones = GetInstance(phoneList, name) If p IsNot Nothing Then retVal = p.SellPrice ' Look at what this does please: ' ' It needs to test to make sure that what was ' returned from 'GetInstance' isn't null (that is, ' that it's not Nothing). ' ' If the return value is null (Nothing) then ' trying to read the "SellPrice" property would ' result in an exception being thrown. End If ' Given that a decimal (the selling price) is a ' value type rather than a reference type, a ' better way to do this would be to have this ' function return a Nullable(Of Decimal). ' ' For simplicity sake I'm not doing that here, but ' keep it in mind. Return retVal End Function Public Shared Function _ GetPhoneNames(ByVal phoneList As List(Of Phones)) As String() ' This is just a convenience method (a function) ' that will return all of the names of the various ' phones as a string array, alphabetized. ' ' I would use LINQ, but the following will ' certainly work: Dim retVal() As String = New String() {} If phoneList IsNot Nothing Then Dim tempList As New List(Of String) For Each p As Phones In phoneList If Not tempList.Contains(p.Name) Then tempList.Add(p.Name) End If Next If tempList.Count > 0 Then tempList.Sort() End If retVal = tempList.ToArray End If Return retVal End Function End Class


    In the form, you can test it like this:

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Dim phoneList As List(Of Phones) Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load TestPhones() End Sub Private Sub TestPhones() phoneList = New List(Of Phones) Phones.AddNew(phoneList, "Basic Blue", 89.5D) Phones.AddNew(phoneList, "Intermediate Intel", 170) Phones.AddNew(phoneList, "Gold Browser", 317.99D) Stop ' When the code gets to "Stop" above, it will halt ' just the same as though you'd put a breakpoint ' in. ' ' When it does, hover your mouse over the variable ' "phoneList" and you'll see that it will show ' three entries. ' ' For each of those, click the "+" symbol to ' expand it and you'll see the phone's name and ' price. ' ' After code execution halts at the "Stop" above, ' continue it and it will then run until it gets ' to the "Stop" below: Dim price As Decimal = _ Phones.GetPhoneSellingPrice(phoneList, "Gold Browser") Stop ' Continue it again to the final "Stop" please: Dim names() As String = _ Phones.GetPhoneNames(phoneList) Stop ' You can use the last method as a way to populate ' your ListBox by using the ListBox's ' .Items.AddRange method. End Sub End Class


    I hope that gives you a starting point. If you're confused on some part of it, just ask. :)


    "Even a dead fish goes with the flow..."


    • Edited by Frank L. Smith Wednesday, December 21, 2016 3:25 PM ...typo in one of the code comments
    • Proposed as answer by Neda Zhang Thursday, December 22, 2016 1:28 AM
    • Marked as answer by William Dishon Friday, December 23, 2016 1:22 AM
    Wednesday, December 21, 2016 2:44 PM

All replies

  • I wanted to make it so when the user selects the phone
    they want from the ListBox and their selected plan, the total cost will be listed at the bottom of the screen

    If P is an integer it can't have a value such as 89.5 or 317.99.

    Your phone class should expose the relevant values as properties so that when you create instances of the class you will be able to access the property (eg, price) to do your calculation.  Your constructor (New sub) will typically pass values that initialise those properties.    I can't tell what you are trying to do with that list of arguments to the constructor, but the typical code would simply initialise the property backing variables from the arguments.  See:
    https://msdn.microsoft.com/en-us/library/xtka85tz.aspx

    Wednesday, December 21, 2016 8:11 AM
  • Hi William Dishon

    Appreciate your initiative !!!

    Take care of the following points when you construct a class

     - It has to represent an Entity

     - It has to define some characteristics

     - It has to expose some behaviors.

    In your case, Phone is an Entity with Characteristics (Model, Plan, Price) & behavior (Calculate).

    - basic blue, intermediate Intel, gold browser are models

    - bronze, silver, gold are plans

    - price is the amount of model & plan

    Calculate is the behavior to


    It all Happenz Sendil

    Wednesday, December 21, 2016 2:16 PM
  • Sendilg,

    Ok. Just realised i'm going to have to make more changes to my code. Thanks for the help :)

    Wednesday, December 21, 2016 2:38 PM
  • William,

    This is a good opportunity to set these up using inheritance. If you think about it, you'll have two classes which will have two members each: The name (of the phone or of the plan) and the price.

    Give the following a look. Hopefully you can see how this first one works (the phones) and with that, set up the other one (the plan):

    Option Strict On Option Explicit On Option Infer Off Public MustInherit Class NameAndPrice Protected _name As String Protected _sellPrice As Decimal Public Sub New(ByVal name As String, _ ByVal price As Decimal) _name = name.Trim _sellPrice = price ' This has a basic fallacy: What if the name is ' null or what if the price is zero (or worse yet, ' negative)? ' ' This area is where it can be handled but for the ' sake of simplicity I’m not doing that here. ' ' Just do know that a ‘real world’ situation would ' need to check for things like this. ' ' Also, there is no test for duplicate entries – ' something else that you’d likely want to do. End Sub ''' <summary> ''' Gets the name associated with this entry. ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property Name As String Get Return _name End Get End Property ''' <summary> ''' Gets the selling price associated with this entry. ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property SellPrice As Decimal Get Return _sellPrice End Get End Property End Class Public Class Phones Inherits NameAndPrice Private Sub New(ByVal name As String, ByVal price As Decimal) MyBase.New(name, price) End Sub Public Shared Sub _ AddNew(ByRef phoneList As List(Of Phones), _ ByVal name As String, _ ByVal sellPrice As Decimal) ' As before, validation should be done here: ' ' What if the phone list (a generic List(Of ' Phone)) is null? ' ' The other two won't need validation here if the ' constructor of the base (abstract) class were ' done correctly. The reason being that otherwise ' it's a duplication of effort. phoneList.Add(New Phones(name, sellPrice)) ' In the one line above, it will take the list ' that you hand it and will add to it an instance ' of the "Phones" class. ' ' You - as the consumer of the class - can't do ' that directly because the constructor is ' private; only a method of the class can directly ' access it. ' ' In the code of your form, you might want to ' experiment with it though - just to show that ' you can't create a new instance of this class ' from there. ' ' In time, when dealing with classes, you'll learn ' about "patterns" and this is a variation of a ' very common pattern known as the "factory ' pattern". End Sub Public Shared Function _ GetInstance(ByVal phoneList As List(Of Phones), _ ByVal name As String) As Phones Dim retVal As Phones = Nothing If phoneList IsNot Nothing AndAlso _ Not String.IsNullOrWhiteSpace(name) Then ' I would normally use LINQ here, but there's ' nothing wrong with simple iteration: For Each p As Phones In phoneList If p.Name = name Then retVal = p Exit For ' Look at this carefully. Do you see what's going ' on here? ' ' It's iterating through every instance of ' "Phones" that's in the list that you give it. ' ' If the member (the property) named "Name" ' matches, it will then set my private variable ' called "retVal" to that instance and, following ' that will then exit the loop. End If Next End If Return retVal End Function Public Shared Function _ GetPhoneSellingPrice(ByVal phoneList As List(Of Phones), _ ByVal name As String) As Decimal ' In all likelihood you’ll be using this function ' more than the ‘GetInstance’ method. ' ' After all, this class only has two members and ' you’re using one of them to find the other, so ' this method makes more sense to use! ;-) ' ' Still though, I’ll make use of the ‘GetInstance’ ' method as shown below: Dim retVal As Decimal = 0 Dim p As Phones = GetInstance(phoneList, name) If p IsNot Nothing Then retVal = p.SellPrice ' Look at what this does please: ' ' It needs to test to make sure that what was ' returned from 'GetInstance' isn't null (that is, ' that it's not Nothing). ' ' If the return value is null (Nothing) then ' trying to read the "SellPrice" property would ' result in an exception being thrown. End If ' Given that a decimal (the selling price) is a ' value type rather than a reference type, a ' better way to do this would be to have this ' function return a Nullable(Of Decimal). ' ' For simplicity sake I'm not doing that here, but ' keep it in mind. Return retVal End Function Public Shared Function _ GetPhoneNames(ByVal phoneList As List(Of Phones)) As String() ' This is just a convenience method (a function) ' that will return all of the names of the various ' phones as a string array, alphabetized. ' ' I would use LINQ, but the following will ' certainly work: Dim retVal() As String = New String() {} If phoneList IsNot Nothing Then Dim tempList As New List(Of String) For Each p As Phones In phoneList If Not tempList.Contains(p.Name) Then tempList.Add(p.Name) End If Next If tempList.Count > 0 Then tempList.Sort() End If retVal = tempList.ToArray End If Return retVal End Function End Class


    In the form, you can test it like this:

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Dim phoneList As List(Of Phones) Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load TestPhones() End Sub Private Sub TestPhones() phoneList = New List(Of Phones) Phones.AddNew(phoneList, "Basic Blue", 89.5D) Phones.AddNew(phoneList, "Intermediate Intel", 170) Phones.AddNew(phoneList, "Gold Browser", 317.99D) Stop ' When the code gets to "Stop" above, it will halt ' just the same as though you'd put a breakpoint ' in. ' ' When it does, hover your mouse over the variable ' "phoneList" and you'll see that it will show ' three entries. ' ' For each of those, click the "+" symbol to ' expand it and you'll see the phone's name and ' price. ' ' After code execution halts at the "Stop" above, ' continue it and it will then run until it gets ' to the "Stop" below: Dim price As Decimal = _ Phones.GetPhoneSellingPrice(phoneList, "Gold Browser") Stop ' Continue it again to the final "Stop" please: Dim names() As String = _ Phones.GetPhoneNames(phoneList) Stop ' You can use the last method as a way to populate ' your ListBox by using the ListBox's ' .Items.AddRange method. End Sub End Class


    I hope that gives you a starting point. If you're confused on some part of it, just ask. :)


    "Even a dead fish goes with the flow..."


    • Edited by Frank L. Smith Wednesday, December 21, 2016 3:25 PM ...typo in one of the code comments
    • Proposed as answer by Neda Zhang Thursday, December 22, 2016 1:28 AM
    • Marked as answer by William Dishon Friday, December 23, 2016 1:22 AM
    Wednesday, December 21, 2016 2:44 PM
  • Frank,

    Thanks for the explanation, I found it really helpful. I'm still however a little confused on some aspects. When I wrote my code for the plan I've ending up doing something wrong when setting up my PlanList on the form. I couldn't figure out what I did wrong. I also couldn't figure out how to properly implement the Items.AddRange method onto the combobox. I've put my code down below for you to look at. Thanks for the help so far!

    Option Strict On
    Option Explicit On
    Option Infer On
    Public MustInherit Class PlanAndPrice
        Protected _Plan As String 'phone plan
        Protected _sellPrice As Decimal 'monthly fee
    
        Public Sub New(ByVal plan As String, ByVal price As Decimal)
    
            _Plan = plan.Trim
            _sellPrice = price
        End Sub
    
        Public ReadOnly Property plan As String
            Get
                Return _Plan 'gets plan associated with entry
            End Get
        End Property
    
        Public ReadOnly Property SellPrice As Decimal
            Get
                Return _sellPrice 'gets selling price associated with entry 
            End Get
        End Property
    End Class
    
    Public Class plans
        Inherits PlanAndPrice
    
        Private Sub New(ByVal plan As String, ByVal price As Decimal)
            MyBase.New(plan, price) 'validation
        End Sub
    
        Public Shared Sub _
                AddNew(ByRef planlist As List(Of plans), ByVal plan As String, ByVal sellprice As Decimal)
            planlist.Add(New plans(plan, sellprice)) 'add list to instance of the "plans" class
        End Sub
    
        Public Shared Function _
                GetInstance(ByVal planlist As List(Of plans), 'iterating through every instance of "plans" thats given in the list 
            ByVal plan As String) As plans
            Dim retVal As plans = Nothing
            If planlist IsNot Nothing AndAlso
                    Not String.IsNullOrWhiteSpace(plan) Then
                For Each p As plans In planlist
                    If p.plan = plan Then
                        retVal = p
                        Exit For
                    End If
                Next
            End If
            Return retVal 'If the property member named "Plan" matches, it will then set the private variable "retVal" to that instannce
            'and, by following that it will then exit the loop
        End Function
    
        Public Shared Function _
                GetPlanSellingPrice(ByVal PlanList As List(Of plans),
                                     ByVal plan As String) As Decimal
            Dim RetVal As Decimal = 0
            Dim p As plans = GetInstance(PlanList, plan)
            If p IsNot Nothing Then
                RetVal = p.SellPrice 'testing to make sure that get instance isn't null 
            End If
            Return RetVal
        End Function
    
        Public Shared Function _
                GetPlanNames(ByVal planlist As List(Of plans)) As String() 'this is a convenience method that returns all plan types as a string array, alphabetized 
            Dim retVal() As String = New String() {}
    
            If planlist IsNot Nothing Then
                Dim tempList As New List(Of String)
    
                For Each p As plans In planlist
                    If Not tempList.Contains(p.plan) Then
                        tempList.Add(p.plan)
                    End If
                Next
                If tempList.Count > 0 Then
                    tempList.Sort()
                End If
                retVal = tempList.ToArray
            End If
    
            Return retVal
    
        End Function
    End Class
    
    

    Option Strict On
    Option Explicit On
    Option Infer Off
    Public MustInherit Class NameAndPrice
        Protected _name As String 'phone type
        Protected _sellPrice As Decimal 'price
        Public Sub New(ByVal name As String,
                      ByVal Price As Decimal)
    
            _name = name.Trim
            _sellPrice = Price
    
        End Sub
    
        Public ReadOnly Property Name As String
            Get
                Return _name 'gets name associated with entry 
            End Get
        End Property
    
        Public ReadOnly Property SellPrice As Decimal
            Get
                Return _sellPrice 'gets selling price associated with entry 
            End Get
        End Property
    End Class
    
    Public Class Phones
        Inherits NameAndPrice
    
        Private Sub New(ByVal name As String, ByVal price As Decimal)
            MyBase.New(name, price) 'validation
        End Sub
    
        Public Shared Sub _
            AddNew(ByRef phoneList As List(Of Phones),
                   ByVal name As String,
                   ByVal sellPrice As Decimal)
    
            phoneList.Add(New Phones(name, sellPrice)) 'adds list to instance of the "phones" class
        End Sub
    
        Public Shared Function _
            GetInstance(ByVal phoneList As List(Of Phones), 'iterating through every instance of the "phones" thats given in the list
                        ByVal name As String) As Phones
    
            Dim retVal As Phones = Nothing
    
            If phoneList IsNot Nothing AndAlso
                Not String.IsNullOrWhiteSpace(name) Then
                For Each p As Phones In phoneList
                    If p.Name = name Then
                        retVal = p
                        Exit For
                    End If
                Next
            End If
    
            Return retVal 'If the property member named "Phones" matches, it will then set the private variable "retVal" to that instannce
            'and, by following that it will then exit the loop
    
        End Function
    
        Public Shared Function _
            GetPhoneSellingPrice(ByVal phoneList As List(Of Phones),
                                 ByVal name As String) As Decimal
            Dim retVal As Decimal = 0
    
            Dim p As Phones = GetInstance(phoneList, name)
    
            If p IsNot Nothing Then
                retVal = p.SellPrice 'testing to make sure that get instance isn't null
            End If
            Return retVal
    
        End Function
        Public Shared Function _
            GetPhoneNames(ByVal phoneList As List(Of Phones)) As String() 'this is a convenience method that returns all phone types as a string array, alphabetized 
            Dim retVal() As String = New String() {}
    
            If phoneList IsNot Nothing Then
                Dim tempList As New List(Of String)
    
                For Each p As Phones In phoneList
                    If Not tempList.Contains(p.Name) Then
                        tempList.Add(p.Name)
                    End If
                Next
    
                If tempList.Count > 0 Then
                    tempList.Sort()
                End If
    
                retVal = tempList.ToArray
            End If
    
            Return retVal
    
        End Function
    End Class
    

    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Product_Purchase_Page
        Dim phoneList As List(Of Phones)
        Dim PlanList As List(Of plans)
        Private Sub Product_Purchase_Page_Load(ByVal sender As System.Object,
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
            TestPhones()
            TestPlans()
    
    
            'Private Sub Button2_Click(sender As Object, e As EventArgs)
            'Form1.Show()
            'Me.Hide()
        End Sub
    
        Private Sub TestPhones()
    
    
            phoneList = New List(Of Phones)
    
            Phones.AddNew(phoneList, "Basic Blue", 89.5D)
            Phones.AddNew(phoneList, "Intermediate Intel", 170)
            Phones.AddNew(phoneList, "Gold Browser", 317.99D)
            Stop
            Dim price As Decimal =
            Phones.GetPhoneSellingPrice(phoneList, "Gold Browser")
            Stop
            Dim names() As String =
                Phones.GetPhoneNames(phoneList)
            Stop
        End Sub
    
        Private Sub TestPlans()
        PlanList = New List(Of plans)
            Plans.AddNew(PlanList, "Bronze", 29)
            Plans.AddNew(PlanList, "Silver", 49)
            Plans.AddNew(PlanList, "Gold", 69)
            Stop
            Dim price As Decimal =
                Plans.GetPlanSellingPrice(PlanList, "Gold")
            Stop
        Dim Plans() As String =
                Plans.GetPlanNames(PlanList)
        Stop
    End Sub
    End Class
    
    'Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
    '    ComboBox2.Items.AddRange(Phones)
    '    "Basic Blue" 
    '        "IntermediateIntel"
    '    "Gold Browser"
    
    'End Sub
    

    Thursday, December 22, 2016 6:34 AM
  • 'Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
    '    ComboBox2.Items.AddRange(Phones)
    '    "Basic Blue" 
    '        "IntermediateIntel"
    '    "Gold Browser"
    
    'End Sub


    To initialise the combobox you need something like:

        ComboBox2.Items.AddRange(Phones.ToArray)

    probably in the form load event handler.  For the selected index changed event you should describe what you expect to happen for that event.

    Thursday, December 22, 2016 8:46 AM
  • William,

    Let's see if the following might help clear things up a bit.

    Using the code for the Phones class from yesterday, I put a ListBox on Form1 (I left the name to the default of "ListBox1").

    Then I used this in the form's code:

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Dim phoneList As List(Of Phones) Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load InitializeData() End Sub Private Sub InitializeData() phoneList = New List(Of Phones) Phones.AddNew(phoneList, "Basic Blue", 89.5D) Phones.AddNew(phoneList, "Intermediate Intel", 170) Phones.AddNew(phoneList, "Gold Browser", 317.99D) ListBox1.Items.AddRange(Phones.GetPhoneNames(phoneList)) End Sub Private Sub _ ListBox1_SelectedIndexChanged(sender As System.Object, _ e As System.EventArgs) _ Handles ListBox1.SelectedIndexChanged If ListBox1.SelectedIndex > -1 Then Dim selected As String = _ ListBox1.Items(ListBox1.SelectedIndex).ToString If Not String.IsNullOrWhiteSpace(selected) Then Dim price As Decimal = _ Phones.GetPhoneSellingPrice(phoneList, selected) MessageBox.Show(String.Format("You selected the {0} phone.{1}{1}The price for this phone is {2:c2}.", _ selected, vbCrLf, price), "Your Choice") End If End If End Sub End Class


    When I first run it, as you can see above it will run the sub "InitializeData" which populates the ListBox:

    Then when I select one of them, I see the following:

    Does that help some?


    "Even a dead fish goes with the flow..."

    Thursday, December 22, 2016 1:08 PM
  • Frank, 

    Yes, that made things a lot clearer. Thanks for the help.

    Friday, December 23, 2016 1:22 AM
  • Frank, 

    Yes, that made things a lot clearer. Thanks for the help.


    I'm glad it helped. :)

    &quot;One who has no vices also has no virtues...&quot;

    Friday, December 23, 2016 11:28 AM