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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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
Transcribed Image Text: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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education