再次感谢! 解决方案 需要注意的几件事,没有特定的顺序.You're With 语句需要在结构成员之前使用 .,例如 .Quantity,而不是 Quantity.您列出的四个错误是由两个原因造成的 - numQuantity 和 numPrice 在您的代码中不存在 - 您可能正在寻找 Quantity 和 Price,您的 TryParse 调用的结果.第 4 个错误是因为您在结构定义中有 productName,而不是 ProductName(注意小写与大写的第一个字母.为避免混淆,我将您的 Quantity 和 Price 变量名称(您在 TryParse 调用中使用的名称)更改为 NewQuantity 和 NewPrice 或类似的东西,以避免与结构 Product 中的 Quantity 和 Price 成员混淆.还有许多其他项目我会采用不同的方式,但由于您正在学习这种语言,您的讲师很可能还没有向您介绍它们.这是您当前代码的修改版本,可以修复您列出的错误:首先,将结构定义中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 StructurePrivate Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close()End SubPrivate 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 SubEnd ClassThen 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 FunctionEnd ClassThe errors being received so far areError 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 StringEnd StructureSecond, use a different variable name for the results of the TryParse calls:Dim newQuantity As LongDim newPrice As DecimalLong.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 IfNext iFourth, 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 = newQuantityEnd With 这篇关于VB程序结构和类数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-26 05:49
查看更多