Daniel Erb
Daniel Erb

Reputation: 61

VB Program Array of Structure and Class

Now before I start I know this is not the most efficient way of doing this program, it is for school.

The project is one that is supposed to calculate what a customer owes by putting the quantity of the item and price per item. So say there are 2 items and 2.50 each. The total due is now 5, next there is one item at 3.00 the total due is now 8.

This would typically be easy by just declaring variables, maybe using a function, a structure OR a class.

Where I am having trouble is that this project requires the use of an array of structure (so that covers using an array and a structure) as well as a class.

When I talked to my instructor he gave me an example of how to possibly use an array in another scenario basically initiating the array with nothing and allowing in a loop for the program to check for UPC. I used that idea and added a text box for product name so if it matches (Say a third item is added in and is the same as item one) then it just adds the quantity to the existing entry in the array. In theory the total due would be just as easy since it can just calculate the quantity and price and add it to the total.

I have not coded the button to clear all variables "new order" as that is very easy.

I have also thoroughly confused myself, I feel due to the unneeded complexity of the program to accomplish such a simple task but here is what I have the main program:

Public Class FrmMain

  Dim order(-1) As product
  Public totalDue As Decimal

  Structure product
    Public Quantity As Long
    Public Price As Decimal
    Public productName As String
  End Structure


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

Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    ' adds the total price to the amount the customer owes

    Dim book As New BookSale
    Dim Quantity As Long
    Dim Price As Decimal


    Long.TryParse(txtQuantity.Text, Quantity)
    Decimal.TryParse(txtPrice.Text, Price)


    'when a user adds an item by id (could be UPC)......  This could be a click event
    'boolean to declare if item was found
    Dim bolFound As Boolean = False
    'upc number of product
    Dim strProduct As String = txtProduct.Text
    'loop through array to see if product has already been added, if so, just update quantity
    For i As Integer = 0 To order.Length - 1
        If order(i).productName = strProduct Then
            Quantity += numQuantity.value
            bolFound = True
            Exit For
        End If
    Next i
    'if product was not found, add it to the array
    If bolFound = False Then
        'never found, add the new item
        ReDim Preserve order(order.Length)
        With order(order.Length - 1)
            ProductName = txtProduct.Text
            Price = numPrice.value
            Quantity = numQuantity.value
        End With
    End If

    totalDue = book.TotalDueTotal
    lblTotalDue.Text = totalDue.ToString("N0")

End Sub
End Class

Then here is the class "bookSale"

Public Class BookSale
 Private _Quantity As Integer
 Private _Price As Decimal

 Public Property TotalDue As Integer
    Get
        Return _Quantity
    End Get
    Set(ByVal value As Integer)
        If value > 0 Then
            _Quantity = value
        Else
            _Quantity = 0
        End If
    End Set
 End Property
 Public Property Price As Decimal
    Get
        Return _Price
    End Get
    Set(ByVal value As Decimal)
        If value > 0 Then
            _Price = value
        Else
            _Price = 0
        End If
    End Set
 End Property

 Public Sub New()
    ' default constructor
    _Quantity = 0
    _Price = 0
 End Sub

 Public Function TotalDueCalc() As Decimal
    Return _Price * _Quantity
 End Function

 Public Function TotalDueTotal() As Decimal
    Dim FinalTotal As Decimal
    Return FinalTotal + TotalDueCalc()
 End Function

End Class

The errors being received so far are Error 3 'numPrice' is not declared. It may be inaccessible due to its protection level. Error 1 'numQuantity' is not declared. It may be inaccessible due to its protection level. Error 4 'numQuantity' is not declared. It may be inaccessible due to its protection level. Error 2 Property 'ProductName' is 'ReadOnly'.

Any help would be greatly appreciated.

P.S. I know some things may be missing like passing variables to the class but I have already played with this for about 3 hours trying to get it to do what I want and I just confused myself way too much. Also yes I am at a relatively beginners level of programming this is my first real programming class and the instructor said we should learn how to do this a little better in the second part of the class dealing with the more advanced aspects of VB.

Thanks again!

Upvotes: 0

Views: 5115

Answers (2)

Tim
Tim

Reputation: 28530

Several things to note, in no particular order.

You're With statement needs to precede the structure members with a ., like .Quantity, not Quantity.

The four errors you listed are because of two reasons - numQuantity and numPrice don't exist in your code - you're probably looking for Quantity and Price, the results of your TryParse calls. The 4th error is because you have productName in the structure definition, not ProductName (note the lower case versus uppercase first letter.

To avoid confusion, I'd change your Quantity and Price variable names (the ones you use in the TryParse calls) to NewQuantity and NewPrice or something like that to avoid confusion with the Quantity and Price members in the structure Product.

There's a number of other items that I would do differently, but since you're learning the language your instructor has most likely not introduced them to you yet. Here's a modified version of your current code that will fix the errors you listed:

First, change the casing of productName to ProductName in your structure definition:

Structure product
    Public Quantity As Long
    Public Price As Decimal
    Public ProductName As String
End Structure

Second, use a different variable name for the results of the TryParse calls:

Dim newQuantity As Long
Dim newPrice As Decimal

Long.TryParse(txtQuantity.Text, newQuantity)
Decimal.TryParse(txtPrice.Text, newPrice)

Third, in your loop to update an existing order, you need to reference the correct Product in the array. Even if you had a value Quantity.value, it wouldn't update the Quantity for that product - you need to tell the program to update order(i)'s Quantity:

For i As Integer = 0 To order.Length - 1
    If order(i).ProductName = strProduct Then
        order(i).Quantity += newQuantity
        bolFound = True
        Exit For
    End If
Next i

Fourth, use the . notation when creating a new Product along with variable names from step 2 above:

With order(order.Length - 1)
    .ProductName = txtProduct.Text
    .Price = newPrice
    .Quantity = newQuantity
End With

Upvotes: 1

OneFineDay
OneFineDay

Reputation: 9024

Rather than using an Array, a List(Of Product) would be a better choice and I also would make it a class and not a structure. No Redim here, just .Add(New Product). The problem your having is your array is set to -1 and your not increasing the size before adding a new element - a List will simplify this process. As for your numQuantity it simply does not exist and the compiler is just letting you know.

Upvotes: 0

Related Questions