我正在尝试制作一个程序来检查所有产品的库存文件,并使用输入的GTIN代码中的正确信息返回(您将(希望)通过查看我的代码了解)
Public Class Form1
Dim FILE_NAME As String = "H:\Visual Studio 2013\Projects\Control Task 2 Barcode Orders\Control Task 2 Barcode Orders\bin\Debug\receipt_orders.txt"
Dim GTIN As String
Dim LineContaining As String
Dim quantity As Integer
Dim description As String
Dim singleamount As String
Dim singleprice As Double
Dim totalprices As Double
Private Sub btnEnterGTIN_Click(sender As Object, e As EventArgs) Handles btnEnterGTIN.Click
GTIN = getValidGTIN()
Call itemfinder(GTIN)
End Sub
Function getValidGTIN()
Dim GTIN, ValidGTINAmountCharacters As String
Dim GTINOK As Boolean
'Declaring variables.
GTINOK = False
Do
GTIN = InputBox("Enter the full GTIN Number (it should be 8 digits long)")
'Prompts the user to enter the GTIN.
ValidGTINAmountCharacters = Len(GTIN)
'Makes it so that the amount of characters of the GTIN is stored in the variable ValidGTINAmountCharacters.
If ValidGTINAmountCharacters = 8 Then GTINOK = True
'Makes it so the program will only accept an input if it was 8 characters long.
If Not IsNumeric(GTIN) Then GTINOK = False
'Makes it so that if any other character typed in apart from a number is not valid.
If Not GTINOK Then MsgBox("The GTIN Number isn't valid. It should be a 8 digit number. (Should not contain letters or symbols).")
'Makes it so that if the GTIN is not valid according to the above, a message appears saying it is invalid.
Loop Until GTINOK
Return GTIN
End Function
Private Sub itemfinder(ByVal GTIN As String)
Using reader As New IO.StreamReader("receipt_orders.txt")
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains(GTIN) Then
line = LineContaining
Exit While
End If
End While
End Using
description = Mid$(LineContaining, 10, 17)
singleamount = Mid$(LineContaining, 38, 4)
quantity = InputBox("Enter the amount required")
totalprices = quantity * singleamount
lstGTIN.Items.Add(GTIN)
lstName.Items.Add(description)
lstQuantity.Items.Add(quantity)
lstSinglePrice.Items.Add(singleamount)
lstTotal.Items.Add(totalprices)
Dim sum As Double
For x As Integer = 0 To lstTotal.Items.Count - 1
sum += Val(lstTotal.Items.Item(x).ToString)
Next
txtTotalPrice.Text = sum.ToString
End Sub
End Class
当我输入数量和物品代码时,我得到一个与总价格计算有关的错误代码 - 我不知道如何解决这个问题! 此外,我使用的文本文件看起来像这样
12345670 L-Shaped Brackets 7.20
10101010 Television 1.80
69696969 Screws 0.20
请尽量解释如何解决这个问题!我不是很熟悉Visual Basic!
答案 0 :(得分:0)
其中的一些代码,例如使用Call
关键字,表示vb6 / vbscript时代的思维方式。这是使用现代和更清晰的VB.Net编码样式的相同功能的实现:
Public Class ProductItem
Public Property GTIN As Integer
Public Property Description As String
Public Property ItemPrice As Decimal
Protected Property SourceData As String
Public Shared Function FromLineString(ByVal lineString As String) As OrderLine
Return New ProductItem() With
{
.Description = lineString.SubString(9,17),
.ItemPrice = Decimal.Parse(lineString.SubString(37,4)),
.GTIN = Int32.Parse(lineString.SubString(0,8)), 'Guessed at this field
.SourceData = lineString
}
End Function
End Class
Public Class Form1
Const FILE_NAME As String = "H:\Visual Studio 2013\Projects\Control Task 2 Barcode Orders\Control Task 2 Barcode Orders\bin\Debug\receipt_orders.txt"
Private Sub btnEnterGTIN_Click(sender As Object, e As EventArgs) Handles btnEnterGTIN.Click
Dim item As ProductItem = itemfinder(Form1.InputValidGTIN())
If item Is Nothing Then Exit Sub ' May want to show error here
Dim QTY As Integer = Form1.InputInteger("Enter the amount required", "Input was not a valid Integer. Please try again.")
Me.SuspendLayout()
lstGTIN.Items.Add(item.GTIN)
lstName.Items.Add(item.Description)
lstQuantity.Items.Add(QTY)
lstSinglePrice.Items.Add(item.ItemPrice)
lstTotal.Items.Add(QTY * item.ItemPrice)
txtTotalPrice.Text = lstTotal.Items.Cast(Of Decimal).Sum().ToString()
Me.ResumeLayout()
End Sub
Public Shared Function InputValidGTIN() As String
Dim GTIN As String = InputBox("Enter the full GTIN Number (it should be 8 digits long)").Trim()
While Not IsValidGTIN(GTIN)
MsgBox("The GTIN Number isn't valid. It should be a 8 digit number. (Should not contain letters or symbols).")
GTIN = InputBox("Enter the full GTIN Number (it should be 8 digits long)").Trim()
End While
Return GTIN
End Function
Public Shared Function IsValidGTIN(ByVal GTIN As String) As Boolean
Static regex As New Regex("^\d{8}$")
Return regex.IsMatch(GTIN)
End Function
Public Shared Function InputInteger(ByVal PromptText As String, ByVal RePromptText As String) As Integer
Dim result As Integer
Dim input As String = InputBox(PromptText)
While Not Int32.TryParse(input, result)
input = InputBox(RePromptText)
End While
Return result
End Function
Private Function itemfinder(ByVal GTIN As String) As ProductItem
Using reader As New IO.StreamReader(FILE_NAME)
Dim line As String = reader.ReadLine()
While line IsNot Nothing
If line.Contains(GTIN) Then
Return ProductItem.FromLineString(line)
End If
line = reader.ReadLine()
End While
End Using
Return Nothing
End Function
End Class