2013-08-17 25 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放在一个真正只需要一个的程序中的复杂性就是我迷失了/困惑的地方。 –