Dim itemPrice As Double = Double.Parse(parts(0)) Dim itemName As String = parts (1) Dim newItem As New TagSale Item(itemPrice. itemName) ReDim Preserve items Sold (i) Exception Unhandled QX items Sold(i) = newItem i += 1 System.IndexOutOfRange Exception: 'Index was outside the bounds of the array. Show Call Stack View Details | Copy Details | Start Live Share session ▷ Exception Settings totalSales += itemPrice totalItems += 1 Loop T Close the file after reading
Hello, I had to edit the code because there were some errors where lines weren't being addresed and issues where Private entitites couldn't be accessed. I have one issue that I cannot solve, which is the Dim itemName As String = parts(1) - I attached this in an image of the error. My current code:
Imports System.IO
Public Class FrmMain
Dim itemsSold() As TagSaleItem ' Array to store TagSaleItem objects
Dim totalSales As Double ' Total amount of money collected
Dim totalItems As Integer ' Total number of items sold
Dim averageSalePrice As Double ' Average sale price
Private Sub FrmMain_Load(sender As Object, a As EventArgs) Handles MyBase.Load
Dim fileName As String = "sales_data.txt"
If File.Exists(fileName) Then
' Open the file for reading
Dim sr As New StreamReader(fileName)
Dim i As Integer = 0
' Read each line and create a new TagSaleItem object with a price and a name, then store it in the array
Do While sr.Peek() >= 0
Dim line As String = sr.ReadLine()
Dim parts() As String = line.Split(",")
Dim itemPrice As Double = Double.Parse(parts(0))
Dim itemName As String = parts(1)
Dim newItem As New TagSaleItem(itemPrice, itemName)
ReDim Preserve itemsSold(i)
itemsSold(i) = newItem
i += 1
totalSales += itemPrice
totalItems += 1
Loop
' Close the file after reading
sr.Close()
End If
' Update the statistics in the GUI
averageSalePrice = totalSales / totalItems
lblTotalSales.Text = "Total Sales: " + totalSales.ToString()
lblTotalItems.Text = "Total Items: " + totalItems.ToString()
lblAverageSalePrice.Text = "Average Sale Price: " + averageSalePrice.ToString()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Dim fileName As String = "sales_data.txt"
' Open the file for writing
Dim sw As New StreamWriter(fileName, False)
' Write each TagSaleItem object's price and name to a new line in a file
For i As Integer = 0 To itemsSold.Length - 1
sw.WriteLine(itemsSold(i).itemPrice.ToString() + "," + itemsSold(i).itemname)
Next
' Close the file after writing
sw.Close()
Me.Close()
End Sub
Private Sub btnSubmitPrice_Click(sender As Object, e As EventArgs) Handles btnSubmitPrice.Click
Dim price As Double = InputBox("Enter Price: ", "Enter a Number")
Dim itemName As String = InputBox("Enter Item Name: ", "Enter a Name")
' Create a new TagSaleItem object with the price and name, then store it in the array and update the statistics
Dim newItem As New TagSaleItem(price, itemName)
ReDim Preserve itemsSold(itemsSold.Length + 1)
itemsSold(itemsSold.Length - 1) = newItem
totalSales += price
totalItems += 1
averageSalePrice = totalSales / totalItems
' Display the statistics
lblTotalSales.Text = "Total Sales: " + totalSales.ToString()
lblTotalItems.Text = "Total Items: " + totalItems.ToString()
lblAverageSalePrice.Text = "Average Sale Price: " + averageSalePrice.ToString()
End Sub
End Class
Public Class TagSaleItem
Public _itemPrice As Double
Public _itemName As String
Public itemname As String
Public Sub New(itemPrice As Double, itemName As String)
Me.itemPrice = itemPrice
Me.itemName = itemName
End Sub
Public Property itemPrice() As Double
Get
Return _itemPrice
End Get
Set(ByVal value As Double)
_itemPrice = value
End Set
End Property
End Class
![Dim itemPrice As Double Double.Parse(parts (0))
Dim itemName As String = parts (1)
Dim newItem As New TagSaleItem(itemprice. itemName)
ReDim Preserve items Sold(i)
Exception Unhandled
items Sold (i) = newItem
i += 1
Loop
1
totalSales += itemPrice
totalItems += 1
Close the file after reading
QX
System.IndexOutOfRange Exception: 'Index was outside the bounds
of the array.
Show Call Stack | View Details | Copy Details | Start Live Share session
▷ Exception Settings](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F6401ea27-421d-498c-85f3-4b9d8126b850%2Fbd1d97ce-492e-4d6e-9287-7d569c841ba7%2Fn3guirn_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)