的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