始める前に、これがこのプログラムを行う最も効率的な方法ではないことを知っています。それは学校のためです.
このプロジェクトは、アイテムの数量とアイテムごとの価格を入力することによって、顧客が負っているものを計算することになっているものです。したがって、2 つのアイテムとそれぞれ 2.50 があるとします。支払総額は現在 5 で、次に 3.00 に 1 つの項目があり、支払総額は 8 です。
これは通常、関数、構造体、またはクラスを使用して、変数を宣言するだけで簡単になります。
私が問題を抱えているのは、このプロジェクトではクラスだけでなく、構造体の配列 (配列と構造体の使用をカバーする) の使用が必要なことです。
私がインストラクターと話をしたとき、彼は別のシナリオで配列を使用する方法の例を教えてくれました. 私はそのアイデアを使用して、製品名のテキスト ボックスを追加したので、一致する場合 (3 番目のアイテムが追加され、アイテム 1 と同じであるとします)、配列内の既存のエントリに数量を追加するだけです。理論的には、数量と価格を計算して合計に追加するだけなので、合計支払額も同様に簡単です。
非常に簡単なので、すべての変数「新しい順序」をクリアするボタンをコーディングしていません。
私はまた、自分自身を完全に混乱させました。このような単純なタスクを達成するためのプログラムの不必要な複雑さのために感じますが、メインプログラムは次のとおりです。
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' です。
どんな助けでも大歓迎です。
PSクラスに変数を渡すなど、いくつかのことが欠けている可能性があることは知っていますが、私はすでに約3時間これを試して、自分がやりたいことをやらせようとしていて、あまりにも混乱しました。また、はい、私はプログラミングの比較的初心者レベルです。これは私の最初の実際のプログラミング クラスであり、インストラクターは、VB のより高度な側面を扱うクラスの第 2 部でこれを行う方法をもう少しよく学ぶべきだと言いました。
再度、感謝します!