2013-08-17 22 views
0

的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的更高級方面更好一點。

再次感謝!

回答

1

有幾件事要注意,沒有特別的順序。

你是With聲明需要在.之前的結構成員之前,如.Quantity,而不是數量。

你列出的四個錯誤是因爲兩個原因 - numQuantitynumPrice不要在你的代碼中存在的 - 你可能尋找QuantityPrice,你TryParse調用的結果。第四錯誤是因爲你的結構定義有productName,不ProductName(注意小寫與大寫首字母。

爲了避免混淆,我會改變你的QuantityPrice變量名(你在使用的那些TryParse調用)NewQuantityNewPrice或類似的東西,以避免與QuantityPrice成員混亂結構Product.

有一個數字,我會做出不同的其他物品,但因爲你是學習語言的教師很可能沒有向你介紹它們,下面是一個修改您當前密碼的IED版本將修正錯誤你上市:

首先,改變productNameProductName套管在你的結構定義:

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

二,結果使用不同的變量名在TryParse電話:

Dim newQuantity As Long 
Dim newPrice As Decimal 

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

第三,在你的循環更新現有的秩序,就需要引用正確Product在數組中。即使你有一個價值Quantity.value,它不會更新該產品的數量 - 你需要告訴程序更新order(i)的數量:

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 

四,使用.符號創建一個新的產品時,以及來自上述步驟2的變量名稱:

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

感謝這工作就像一個魅力,我想變化的變化將工作,但我知道這不是問題的核心。感謝第三次和第四次修復,儘管這是我困惑的地方,因爲那是我無法工作的主要部分。 –

+0

@DanielErb - 不客氣。祝你好運,在你的課堂上還有快樂的編碼:) – Tim

0

而不是使用陣列,List(Of Product)將是一個更好的選擇,我也會使它class而不是structure。沒有Redim這裏,只是.Add(New Product)。你的問題是你的陣列設置爲-1,並且在添加新元素之前不增加尺寸 - List將簡化此過程。至於你的numQuantity它根本不存在,編譯器只是讓你知道。

+0

問題在於需求設置爲需要使用數組以及結構和類。 我可以用閉眼只用一個結構或只是一個班來完成程序。試圖將所有3放在一個真正只需要一個的程序中的複雜性就是我迷失了/困惑的地方。 –