Results 1 to 8 of 8

Thread: HELP fixing Depreciation Program

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    2

    Angry HELP fixing Depreciation Program

    This program is for my VBII course. I thought I had coded everything correctly, but I keep getting a 'System.IndexOutOfRangeException' at line 35. ( _strInventoryItem(intCount) = objReader.ReadLine())
    Can anyone give me some insight into what I did wrong?



    Code:
    ' Program Name: Trends Menswear Depreciation Windows App
    ' Author: Natalie Hillard
    ' Date: Febuary 1, 2015
    ' Purpose: This app determines the depreciation based on
    '          a 5 year life on inventory items using the
    '          straight-line and double-declining methods.
    
    Option Strict On
    
    Public Class frmDepreciation
    
        ' Class-level variables
        Private _intLifeOfItems As Integer = 5
        Public Shared _intSizeOfArray As Integer = 7
        Public Shared _strInventoryItem(_intSizeOfArray) As String
        Private _strItemId(_intSizeOfArray) As String
        Private _decInitialPrice(_intSizeOfArray) As Decimal
        Private _intQuantity(_intSizeOfArray) As Integer
    
    
    
        Private Sub frmDepreciation_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'read the txtfile and populate inventory lstbox
    
            'initalize StreamReader
            Dim objReader As IO.StreamReader
            Dim strLocationAndNameOfFile As String = "D:\inventory.txt"
            Dim intCount As Integer = 0
            Dim intFill As Integer
            Dim strFileError As String = "The file is not available. Restart when file is located."
    
            'verify file exists
            If IO.File.Exists(strLocationAndNameOfFile) Then
                objReader = IO.File.OpenText(strLocationAndNameOfFile)
                'read line by line
                Do While objReader.Peek <> -1
                    _strInventoryItem(intCount) = objReader.ReadLine()
                    _strItemId(intCount) = objReader.ReadLine()
                    _decInitialPrice(intCount) = Convert.ToDecimal(objReader.ReadLine())
                    _intQuantity(intCount) = Convert.ToInt32(objReader.ReadLine())
                    intCount += 1
                Loop
                objReader.Close()
    
                'listbox is filled with ids
                For intFill = 0 To (_strItemId.Length - 1)
                    lstInventoryId.Items.Add(_strItemId)
                Next
            Else
                MsgBox(strFileError, , "Error")
                Close()
            End If
    
        End Sub
    
        Private Sub btnCalculateDepreciation_Click(sender As Object, e As EventArgs) Handles btnCalculateDepreciation.Click
            ' button calls the depreciation Sub procedures
            'declare variables
            Dim intSelectedItemId As Integer
            Dim strMissingSelection As String = "Missing Selection"
            Dim strSelectDepreciationError As String = "Select a Depreciation Method"
            Dim strSelectInventoryIdError As String = "Select an Inventory Item ID"
    
            'if the an inventory item and depreciation rad button are selected.
            'call depreciation procedures
            If lstInventoryId.SelectedIndex >= 0 Then
                intSelectedItemId = lstInventoryId.SelectedIndex
                If radStraightLine.Checked Then
                    StraightLineDepreciation(intSelectedItemId)
                ElseIf radDoubleDeclining.Checked Then
                    DoubleDecliningDepreciation(intSelectedItemId)
                Else
                    MsgBox(strSelectDepreciationError, , strMissingSelection)
                End If
            Else
                MsgBox(strSelectInventoryIdError, , strMissingSelection)
            End If
    
        End Sub
        Private Sub StraightLineDepreciation(ByVal intItemId As Integer)
            'computes and displays straight-line depreciation
            'declare variables
            Dim intStraightPresentYear As Integer
            Dim decStraightPresentYearValue As Decimal = 0
            Dim decStraightDepreciation As Decimal
            Dim decStraightTotal As Decimal
            Dim strDepreciationItem As String = "The depreciation of the item: "
            Dim strQuantityMessage As String = "Quantity: "
    
            'procedure MakeObjectsVisible is called to display form objects
            MakeObjectsVisible()
            ' display the item and quantity of selected item
            lblItem.Text = strDepreciationItem & _strInventoryItem(intItemId)
            lblQuantity.Text = strQuantityMessage & _intQuantity(intItemId).ToString()
            ' straight-line formula:
            decStraightDepreciation = _decInitialPrice(intItemId) / _intLifeOfItems
            decStraightPresentYearValue = _decInitialPrice(intItemId)
    
            'loop repeats for life of items (5yrs)
            For intStraightPresentYear = 1 To _intLifeOfItems
                'accumulates total depreciation
                decStraightTotal += decStraightDepreciation
                'displays amounts
                lstYear.Items.Add(intStraightPresentYear.ToString())
                lstPresentValue.Items.Add(decStraightPresentYearValue.ToString("C"))
                lstYearDepreciation.Items.Add(decStraightDepreciation.ToString("C"))
                lstTotalDepreciation.Items.Add(decStraightTotal.ToString("C"))
                decStraightPresentYearValue -= decStraightDepreciation
            Next
    
    
        End Sub
    
        Private Sub DoubleDecliningDepreciation(ByRef intItemId As Integer)
            'computes and displays double declining depreciation
            'declare variables
            Dim intDoublePresentYear As Integer
            Dim decDoublePresentYearValue As Decimal = 0
            Dim decDoubleDepreciation As Decimal
            Dim decDoubleTotal As Decimal
    
            'procedure is called to make onjects visible
            MakeObjectsVisible()
            'display item and quantity of selection
            lblItem.Text = "The depreciation of the item: " & _strInventoryItem(intItemId)
            lblQuantity.Text = "Quantity: " & _intQuantity(intItemId).ToString()
            decDoublePresentYearValue = _decInitialPrice(intItemId)
    
            'loop for life of items
            For intDoublePresentYear = 0 To _intLifeOfItems
                'DD formula
                decDoubleDepreciation = (decDoublePresentYearValue * 2D) / _intLifeOfItems
                'accumulates the total of depreciation
                decDoubleTotal += decDoubleDepreciation
    
                'displays amounts
                lstYear.Items.Add(intDoublePresentYear.ToString())
                lstPresentValue.Items.Add(decDoublePresentYearValue.ToString("C"))
                lstYearDepreciation.Items.Add(decDoubleDepreciation.ToString("C"))
                lstTotalDepreciation.Items.Add(decDoubleTotal.ToString("C"))
                decDoublePresentYearValue -= decDoubleDepreciation
            Next
        End Sub
        Private Sub MakeObjectsVisible()
            'this procedure displays the objects that show the results
            lblItem.Visible = True
            lblQuantity.Visible = True
            lblYear.Visible = True
            lstYear.Visible = True
            lblPresentValue.Visible = True
            lstPresentValue.Visible = True
            lblYearDepreciation.Visible = True
            lstYearDepreciation.Visible = True
            lblTotalDepreciation.Visible = True
            lstTotalDepreciation.Visible = True
            ' previous data is removed
            lstYear.Items.Clear()
            lstPresentValue.Items.Clear()
            lstYearDepreciation.Items.Clear()
            lstTotalDepreciation.Items.Clear()
    
        End Sub
    
        Private Sub mnuDisplay_Click(sender As Object, e As EventArgs) Handles mnuDisplay.Click
            Dim frmSecond As New frmDisplayInventory
    
            'hide current form
            Hide()
            frmSecond.ShowDialog()
        End Sub
    
        Private Sub mnuClear_Click(sender As Object, e As EventArgs) Handles mnuClear.Click
            'resets values`
            lstInventoryId.SelectedIndex = -1
            radStraightLine.Checked = False
            radDoubleDeclining.Checked = False
            lblItem.Visible = False
            lblQuantity.Visible = False
            lblYear.Visible = False
            lstYear.Visible = False
            lblPresentValue.Visible = False
            lstPresentValue.Visible = False
            lblYearDepreciation.Visible = False
            lstYearDepreciation.Visible = False
            lblTotalDepreciation.Visible = False
            lstTotalDepreciation.Visible = False
            lstInventoryId.Items.Clear()
            lstPresentValue.Items.Clear()
            lstTotalDepreciation.Items.Clear()
            lstYear.Items.Clear()
            lstYearDepreciation.Items.Clear()
        End Sub
    
        Private Sub mnuExit_Click(sender As Object, e As EventArgs) Handles mnuExit.Click
            'closes app
            Application.Exit()
        End Sub
    End Class

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: HELP fixing Depreciation Program

    You must have more than 8 lines in your file.

    Public Shared _intSizeOfArray As Integer = 7
    Public Shared _strInventoryItem(_intSizeOfArray) As String

    _strInventoryItem has 8 elements 0 to 7. if intCount > 7 you'll get a 'System.IndexOutOfRangeException' at line 35.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    2

    Re: HELP fixing Depreciation Program

    Quote Originally Posted by .paul. View Post
    You must have more than 8 lines in your file.

    Public Shared _intSizeOfArray As Integer = 7
    Public Shared _strInventoryItem(_intSizeOfArray) As String

    _strInventoryItem has 8 elements 0 to 7. if intCount > 7 you'll get a 'System.IndexOutOfRangeException' at line 35.
    Thanks for the reply!

    The text file was provided along with the assignment which included all the data for Inventory items, prices, and IDs. They each have 8 entries. Like so:

    Point of Sale Register
    A101
    1599.99
    12
    Store Display Racks
    D208
    49.99
    112
    Mannequins
    D209
    345.00
    41
    End Cap Displays
    D210
    118.99
    16
    Hangars
    D211
    1.19
    1200
    Office Computers
    E101
    629.99
    11
    Chairs
    F310
    145.00
    32
    Sofa
    F311
    829.99
    6



    So are you suggesting I should break it up into separate text files for the relevant categories? Im sorry if that is a silly question, but Im not sure how to proceed.

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,612

    Re: HELP fixing Depreciation Program

    So are you suggesting I should break it up into separate text files for the relevant categories?
    No. You've missed the point of the problem.

    The problem is that you've got a file with 8 rows in it and you're trying to fit those rows into an array which can only contain 7 elements. The eighth element doesn't fit so you're getting that exception. There are a couple of possible solutions:-
    1. Remove the eighth record from the file
    2. Make the array one element bigger by declaring _intSizeOfArray as 8 instead of 7
    3. An even better solution would be to use a list instead of an array because that will resize automatically so you could accommodate any number of elements in the file. If this is for a class, though, and you haven't been introduced to lists yet you should probably ignore this suggestion.

    .Paul., am I going crazy or was this a slip up: "_strInventoryItem has 8 elements 0 to 7". I think it will have 7 elements 0 to 6. Zero basing of arrays affects their indexing but not their size. You've got me questioning my sanity, though, because if it's a slip it's an uncharacteristic one.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: HELP fixing Depreciation Program

    make sure there's no empty line at the bottom of the file

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: HELP fixing Depreciation Program

    Code:
    Public Shared _intSizeOfArray As Integer = 7
    Public Shared _strInventoryItem(_intSizeOfArray) As String
    is equivalent to:

    Code:
    Public Shared _strInventoryItem(7) As String
    0 to 7 = 8 elements

  7. #7
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,612

    Re: HELP fixing Depreciation Program

    0 to 7 = 8 elements
    Just took a look at MSDN and you're right.

    I almost exclusively use c# these days and this: string myArray = new string[n]; create an array of n elements (because n defines the size). In VB this: dim myArray(n) as string creates an array of n+1 elements (because n defines the upper index). I'm quite surprised at that difference.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: HELP fixing Depreciation Program

    Yes. You would have been right if we were talking about C#...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width