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!
No comments:
Post a Comment