none
Help with communication between multiple classes RRS feed

  • Question

  • Here's the instructions we were given for this activity:

    1. Create an Item Class. It should be able to store one string, one integer and one double.
    2. Create an ItemList Class. This class should contain an array of Items. You may hard code the number of items in the initial array or use ReDim to increase the size of the array before you insert new items into the array. This class should allow for expanding the size of the array if necessary and must include a function to generate a receipt as a string for all of the items in the Item array.
    3. Create an application interface that asks the user to enter 3 values: text string (Part Name), a quantity, and a sale's' price.
    4. Once the 3 values are all entered, the user can press a button. Store these values in the array of Item class objects in the ItemList. Clear the text boxes for the next item.
    5. When the user is done entering N items into the ItemList, they may push a second button in the UI to save a receipt. The string of text generated by the button push should call the function in the ItemList class that creates a receipt as a string and save that string to a file with the name Receipt_DATE.txt where DATE is todays date as YYYY-MM-DD.


    The part im stuck on is how to send my 3 values to the ItemList class, as well as how to increase my shopping cart count by one.

    Public Class Form1

        Private ShoppingCart As New ItemList
        Private Counter As Integer = 0
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""


        End Sub




        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim n As String
            Dim q As Integer
            Dim p As Double
            Dim i As Integer
            If Not TextBox1.Text = "" Then


                n = TextBox1.Text
                ShoppingCart.Items(Counter).PartName = n

                q = TextBox2.Text
                ShoppingCart.Items(Counter).Quantity = q

                p = TextBox3.Text
                ShoppingCart.Items(Counter).Price = p

                TextBox1.Text = ""
                TextBox2.Text = ""
                TextBox3.Text = ""

                Counter = i + 1
                Counter = Label7.Text
                'counter here
            End If
        End Sub



    • Edited by Henry Tyson Thursday, March 22, 2018 12:21 AM
    Thursday, March 22, 2018 12:19 AM

All replies

  • The part im stuck on is how to send my 3 values to the ItemList class

    You can't, because the ItemList class is a collection of Items, not a collection of strings or numbers..  You can only store something in the collection that is the right type for the collection, and that type is Item.  So the instruction should read:

    "Store these values in a new instance of the Item class.   Add that instance of the Item class to the collection in the ItemList. Clear the text boxes for the next item."

    You defined the Item class for the purpose of storing this data as a single object, which is something you can store in the collection you created.

    An array is a really bad choice for the ItemList collection, although it is the choice implied by the question.  The reason is the problem you have run into - you do not know ahead of time how many you will need to store. .Net supports collections that don't have this problem, such as a simple List(Of T) which is a lot like an array, or a Dictionary(Of T, Key) if you want to access items by a key, and many more.

    In this case it is an array, so you will use ReDim with the Preserve option.  You can use the present size of the array to decide what the new size will be (eg, 1 more).
    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/redim-statement

    https://msdn.microsoft.com/en-us/library/system.array.length(v=vs.110).aspx


    • Edited by Acamar Thursday, March 22, 2018 12:53 AM sp
    Thursday, March 22, 2018 12:52 AM
  • Hi

    Why you are being taught to use an array is beyond me - it is probably the worst collection type you could use.

    Anyway, here is a start. This example is bare bones, but, it does illustrate the fact that you need to change the type from string to a numeric type (Integer, Double) to store them as such.

    This example only partially answers your question, and is not suitable for you to submit as your own work, but serves as a 'push' in one of the many possible ways you can start your project.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim Items() As Item
      Dim current As Integer = 0
      Function GetInteger(s As String) As Integer
    	Dim v As Integer
    	If Integer.TryParse(s, v) Then Return v
    
    	' string was NOT a valid Integer, so return ZERO
    	Return 0
      End Function
      Function GetDouble(s As String) As Double
    	Dim v As Double
    	If Double.TryParse(s, v) Then Return v
    
    	' string was NOT a valid Double, so return ZERO
    	Return 0D
      End Function
    
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Dim item As New Item
    	With item
    	  .MyInteger = GetInteger(TextBox1.Text)
    	  .MyString = TextBox2.Text
    	  .MyDouble = GetDouble(TextBox3.Text)
    	End With
    	ReDim Preserve Items(current)
    	Items(current) = item
    	current += 1
    
    	TextBox1.Clear()
    	TextBox2.Clear()
    	TextBox3.Clear()
      End Sub
    End Class
    
    Class Item
      Property MyInteger As Integer
      Property MyString As String
      Property MyDouble As Double
    End Class


    Regards Les, Livingston, Scotland

    Thursday, March 22, 2018 12:57 AM
  • Hi

    Why you are being taught to use an array is beyond me - it is probably the worst collection type you could use.


    Regards Les, Livingston, Scotland

    My guess is the instructor isn't a hands on programmer really.

    La vida loca

    Thursday, March 22, 2018 1:14 AM
  • So in the item class i actually "Dim" the items as string, integer, and double for the specific partname, quantity and price. is that not applicable?
    Thursday, March 22, 2018 3:10 PM
  • This answers a few of the questions I was having. im still getting the error "reference to a non shared member requires an object reference" for each of the places textbox1-2-3 are referenced.

     

    Thursday, March 22, 2018 3:55 PM
  • He is not, he told us yesterday that if we had a question, then google it.
    Thursday, March 22, 2018 3:55 PM
  • Henry - short example

    The two classes.  Note that I used a List rather than array as some others have pointed out.  Also, because this is about money, Decimal is a better data type than double(IMO).

    Class Item Property MyInteger As Integer = Integer.MinValue Property MyString As String = "" Property MyDecimal As Decimal = Decimal.MinValue Public Sub New() End Sub Public Sub New(anInt As Integer, aString As String, aDecimal As Decimal) Me.MyInteger = anInt Me.MyString = aString Me.MyDecimal = aDecimal End Sub End Class

    Class ItemList Private ListOfItems As New List(Of Item) Public Sub AddItem(ItemToAdd As Item) Me.ListOfItems.Add(ItemToAdd) End Sub Public Function CreateReceipt() As String Dim tot As Decimal For Each itm As Item In Me.ListOfItems tot += itm.MyInteger * itm.MyDecimal Next Dim rv As String = String.Format("Total: {0:c2}", tot) Return rv End Function End Class

    Some code to test with.

            Dim myitms As New ItemList
            Dim itm As New Item(1, "One", 1.1D)
            myitms.AddItem(itm)
            itm = New Item(2, "Two", 2.2D)
            myitms.AddItem(itm)
            itm = New Item(3, "Three", 3.03D)
            myitms.AddItem(itm)
            itm = New Item(4, "Four", 4.04D)
            myitms.AddItem(itm)
            Dim rcpt As String = myitms.CreateReceipt
            Stop 'look at rcpt


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.


    • Edited by dbasnett Thursday, March 22, 2018 8:09 PM
    Thursday, March 22, 2018 5:23 PM
  • He is not, he told us yesterday that if we had a question, then google it.

    Henry,

    If you follow a course it is not unusual to let the student it first do in a difficult way. 

    Which is what you are doing currently. 

    Most of us can not easily help you because they have abandoned that way. 

    Maybe can you tell your teacher that you have Googled and that your perception is that this way is not the right way but there are better ways currently. Two chances, he says yes or says no. 

    The answer tells you then enough.  

    :-)



    Success
    Cor

    Thursday, March 22, 2018 7:12 PM
  • Yes, I agree, ive already emailed him with my attempt and told him that ive been searching on google for possible solutions. Thought if post on here as well to see if anyone else had any useful insight.

    Thursday, March 22, 2018 7:27 PM
  • So in the item class i actually "Dim" the items as string, integer, and double for the specific partname, quantity and price. is that not applicable?

    You create a new instance of the item class:

        Dim thisItem as New Item

    and set the values for the properties of that class from the user input:

        thisItem.PartName = n
        thisItem.Quantity = q
        thisItem.Price = p

    so that you can add that new instance of the item class to the collection:

        ShoppingCart.Items(Counter) = thisItem

    • Proposed as answer by Stanly Fan Friday, March 23, 2018 2:53 AM
    Thursday, March 22, 2018 9:05 PM