的VB程序矩陣現在開始之前,我,我知道這是不是做這個節目的最有效的方法,它是學校。結構和
該項目是一個應該計算一下客戶欠下通過將項目的數量和價格每件。所以說有2個項目和2.50每個。現在由於總量是5,接下來是在3.00總由於現在是8
這通常是由剛剛聲明變量,可能使用功能,結構或一類簡單的一個項目。
如果我有麻煩的是,這個項目需要使用結構的陣列(以便覆蓋使用數組和結構),以及一類。
當我跟我的教練,他給了我如何在另一種情況基本上開始一無所有的陣列,並允許在程序檢查UPC循環可能要使用的一個陣列的例子。我使用了這個想法,並添加了一個產品名稱的文本框,如果它匹配(說第三個項目被添加並與第一個項目相同),那麼它只是將數量添加到數組中的現有條目。從理論上講,總價應該是一樣容易的,因爲它可以計算數量和價格並將其加到總量中。
我還沒有編碼按鈕,清除所有變量的「新秩序」,因爲這是很容易的。
我也徹底混淆自己,我覺得由於程序的不必要的複雜性來完成這樣一個簡單的任務,但這裏是我的主要程序:
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
當年這裏是類「 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
正在接收的錯誤至今都 錯誤3 'numPrice' 未聲明。由於其保護級別,它可能無法訪問。 錯誤1'numQuantity'未被聲明。由於其保護級別,它可能無法訪問。 錯誤4'numQuantity'未被聲明。由於其保護級別,它可能無法訪問。 錯誤2屬性'ProductName'爲'ReadOnly'。
任何幫助將不勝感激。
P.S.我知道有些東西可能會丟失,比如向班級傳遞變量,但我已經玩了大約3個小時,試圖讓它做我想做的事情,我只是把自己的方式弄得太過分了。 也是的,我在一個相對初學者的編程水平這是我的第一個真正的編程類和教練說,我們應該學習如何做到這一點在類的第二部分處理VB的更高級方面更好一點。
再次感謝!
感謝這工作就像一個魅力,我想變化的變化將工作,但我知道這不是問題的核心。感謝第三次和第四次修復,儘管這是我困惑的地方,因爲那是我無法工作的主要部分。 –
@DanielErb - 不客氣。祝你好運,在你的課堂上還有快樂的編碼:) – Tim