VB程序结构和类数组 [英] VB Program Array of Structure and Class

查看:25
本文介绍了VB程序结构和类数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在在我开始之前,我知道这不是执行此计划的最有效方式,它适用于学校.

该项目应该通过输入商品的数量和每件商品的价格来计算客户的欠款.所以说有 2 个项目,每个项目 2.50.总到期现在是 5,接下来在 3.00 有一个项目,总到期现在是 8.

这通常很容易,只需声明变量,可能使用函数、结构或类.

我遇到问题的地方是这个项目需要使用一个结构数组(因此涵盖了使用数组和结构)以及一个类.

当我和我的导师交谈时,他给了我一个例子,说明如何在另一个场景中使用数组,基本上是什么都不启动数组,并允许程序在循环中检查 UPC.我使用了这个想法并为产品名称添加了一个文本框,因此如果它匹配(假设添加了第三个项目并且与第一个项目相同),那么它只会将数量添加到数组中的现有条目中.理论上,应付总额也很容易,因为它可以计算数量和价格并将其添加到总额中.

我没有编写按钮来清除所有变量新订单",因为这很容易.

我也把自己弄糊涂了,我觉得由于程序的复杂性不需要完成这样一个简单的任务,但这是我的主程序:

公共类FrmMainDim order(-1) 作为产品公共总计到期为十进制结构产品公众号只要以十进制表示的公共价格公共产品名称作为字符串端部结构Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 btnExit.Click我.Close()结束子Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) 处理 btnAdd.Click' 将总价与客户欠款相加昏暗的书作为新书销售昏暗的数量一样长Dim 价格为十进制Long.TryParse(txtQuantity.Text, Quantity)Decimal.TryParse(txtPrice.Text, Price)'当用户通过 id(可能是 UPC)添加一个项目时......这可能是一个点击事件'boolean 声明是否找到项目Dim bolFound As Boolean = False'upc 产品编号Dim strProduct As String = txtProduct.Text'遍历数组查看产品是否已添加,如果已添加,只需更新数量For i As Integer = 0 To order.Length - 1如果 order(i).productName = strProduct Then数量 += numQuantity.valuebolFound = 真退出万一接下来我'如果未找到产品,则将其添加到数组中如果 bolFound = False 那么'从未找到,添加新项目ReDim 保留顺序(order.Length)有订单(order.Length - 1)产品名称 = txtProduct.Text价格 = numPrice.value数量 = numQuantity.value结束于万一totalDue = book.TotalDueTotallblTotalDue.Text = totalDue.ToString("N0")结束子结束类

然后是bookSale"类

公开课图书销售Private _Quantity 作为整数Private _Price 作为十进制公共属性 TotalDue 作为整数得到返回_数量结束获取Set(ByVal value As Integer)如果值>0 那么_数量 = 价值别的_数量 = 0万一结束集最终财产公共财产价格为十进制得到返回_价格结束获取Set(ByVal value As Decimal)如果值>0 那么_价格=价值别的_价格 = 0万一结束集最终财产公共子新建()' 默认构造函数_数量 = 0_价格 = 0结束子公共函数 TotalDueCalc() 作为十进制返回_价格 * _数量结束函数公共函数 TotalDueTotal() 作为十进制Dim FinalTotal 为十进制返回 FinalTotal + TotalDueCalc()结束函数结束类

目前收到的错误是错误 3 'numPrice' 未声明.由于其保护级别,它可能无法访问.错误 1 ​​'numQuantity' 未声明.由于其保护级别,它可能无法访问.错误 4 'numQuantity' 未声明.由于其保护级别,它可能无法访问.错误 2 属性ProductName"是ReadOnly".

任何帮助将不胜感激.

附言我知道有些东西可能会丢失,比如将变量传递给类,但我已经玩了大约 3 个小时试图让它做我想做的事情,我只是让自己太困惑了.另外是的,我处于相对初学者的编程水平,这是我第一次真正的编程课,讲师说我们应该在课程的第二部分学习如何更好地完成这一点,以处理 VB 的更高级方面.

再次感谢!

解决方案

需要注意的几件事,没有特定的顺序.

You're With 语句需要在结构成员之前使用 .,例如 .Quantity,而不是 Quantity.

您列出的四个错误是由两个原因造成的 - numQuantitynumPrice 在您的代码中不存在 - 您可能正在寻找 QuantityPrice,您的 TryParse 调用的结果.第 4 个错误是因为您在结构定义中有 productName,而不是 ProductName(注意小写与大写的第一个字母.

为避免混淆,我将您的 QuantityPrice 变量名称(您在 TryParse 调用中使用的名称)更改为 NewQuantityNewPrice 或类似的东西,以避免与结构 Product 中的 QuantityPrice 成员混淆.

还有许多其他项目我会采用不同的方式,但由于您正在学习这种语言,您的讲师很可能还没有向您介绍它们.这是您当前代码的修改版本,可以修复您列出的错误:

首先,将结构定义中productName 的大小写更改为ProductName:

结构产品公众号只要以十进制表示的公共价格公共产品名称作为字符串端部结构

其次,对 TryParse 调用的结果使用不同的变量名称:

Dim newQuantity As LongDim newPrice 为十进制Long.TryParse(txtQuantity.Text, newQuantity)Decimal.TryParse(txtPrice.Text, newPrice)

第三,在更新现有订单的循环中,您需要引用数组中正确的Product.即使您有一个值 Quantity.value,它也不会更新该产品的 Quantity - 您需要告诉程序更新 order(i) 的 Quantity:

For i As Integer = 0 To order.Length - 1如果 order(i).ProductName = strProduct 那么order(i).Quantity += newQuantitybolFound = 真退出万一接下来我

第四,在创建新产品时使用 . 符号以及上面步骤 2 中的变量名称:

有订单(order.Length - 1).ProductName = txtProduct.Text.价格=新价格.Quantity = newQuantity结束于

Now before I start I know this is not the most efficient way of doing this program, it is for school.

The project is one that is supposed to calculate what a customer owes by putting the quantity of the item and price per item. So say there are 2 items and 2.50 each. The total due is now 5, next there is one item at 3.00 the total due is now 8.

This would typically be easy by just declaring variables, maybe using a function, a structure OR a class.

Where I am having trouble is that this project requires the use of an array of structure (so that covers using an array and a structure) as well as a class.

When I talked to my instructor he gave me an example of how to possibly use an array in another scenario basically initiating the array with nothing and allowing in a loop for the program to check for UPC. I used that idea and added a text box for product name so if it matches (Say a third item is added in and is the same as item one) then it just adds the quantity to the existing entry in the array. In theory the total due would be just as easy since it can just calculate the quantity and price and add it to the total.

I have not coded the button to clear all variables "new order" as that is very easy.

I have also thoroughly confused myself, I feel due to the unneeded complexity of the program to accomplish such a simple task but here is what I have the main program:

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

Then here is the 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

The errors being received so far are Error 3 'numPrice' is not declared. It may be inaccessible due to its protection level. Error 1 'numQuantity' is not declared. It may be inaccessible due to its protection level. Error 4 'numQuantity' is not declared. It may be inaccessible due to its protection level. Error 2 Property 'ProductName' is 'ReadOnly'.

Any help would be greatly appreciated.

P.S. I know some things may be missing like passing variables to the class but I have already played with this for about 3 hours trying to get it to do what I want and I just confused myself way too much. Also yes I am at a relatively beginners level of programming this is my first real programming class and the instructor said we should learn how to do this a little better in the second part of the class dealing with the more advanced aspects of VB.

Thanks again!

解决方案

Several things to note, in no particular order.

You're With statement needs to precede the structure members with a ., like .Quantity, not Quantity.

The four errors you listed are because of two reasons - numQuantity and numPrice don't exist in your code - you're probably looking for Quantity and Price, the results of your TryParse calls. The 4th error is because you have productName in the structure definition, not ProductName (note the lower case versus uppercase first letter.

To avoid confusion, I'd change your Quantity and Price variable names (the ones you use in the TryParse calls) to NewQuantity and NewPrice or something like that to avoid confusion with the Quantity and Price members in the structure Product.

There's a number of other items that I would do differently, but since you're learning the language your instructor has most likely not introduced them to you yet. Here's a modified version of your current code that will fix the errors you listed:

First, change the casing of productName to ProductName in your structure definition:

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

Second, use a different variable name for the results of the TryParse calls:

Dim newQuantity As Long
Dim newPrice As Decimal

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

Third, in your loop to update an existing order, you need to reference the correct Product in the array. Even if you had a value Quantity.value, it wouldn't update the Quantity for that product - you need to tell the program to update order(i)'s Quantity:

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

Fourth, use the . notation when creating a new Product along with variable names from step 2 above:

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

这篇关于VB程序结构和类数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆