7 Replies - 771 Views - Last Post: 18 January 2012 - 07:31 AM Rate Topic: -----

#1 oldnewbie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 16-December 11

Array questions

Posted 17 January 2012 - 08:32 PM

Hi, I have a couple of questions regarding arrays. I'm using Streamwriter/Streamreader for the first time in this project and keep getting an "Argument out of Range Exception was unhandled" error. It says "length cannot be less than zero. Parameter name: length." This happens at "dblYearlyIncome."

Also, how do I find out the number of occurrences of an item in an array? For instance, I have a Race combobox and need to find out how many occurrences there are for each race. Also, is there a way to count the number of occurrences of an array itself, I guess the upper index value? Thank you.


Private Sub btnLoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadData.Click

        'Declare the EmpFile as an input file
        Dim EmpFile As System.IO.StreamReader
        Dim strFileName As String = "employees2.txt"

        Dim strEmpRecord As String
        Dim intIndex As Integer
        Dim intCount As Integer

        Dim intRand() As Integer
        Dim strDate() As String
        Dim strCountyState() As String
        Dim strRace() As String
        Dim intNumberInHousehold() As String
        Dim dblYearlyIncome() As Double


        'Determine if the file exists
        'If it does it will open
        'If not, it will display a message box
        If System.IO.File.Exists(strFileName) Then
            EmpFile = System.IO.File.OpenText(strFileName)

            Do Until EmpFile.Peek = -1
                'Read the record(s) from the "employees2.txt" file
                strEmpRecord = EmpFile.ReadLine()

                'Re-declare the array
                ReDim Preserve intRand(intCount)
                ReDim Preserve strDate(intCount)
                ReDim Preserve strCountyState(intCount)
                ReDim Preserve strRace(intCount)
                ReDim Preserve intNumberInHousehold(intCount)
                ReDim Preserve dblYearlyIncome(intCount)

                'Break the record apart into fields
                intIndex = strEmpRecord.IndexOf(",")
                intRand(intCount) = CInt(strEmpRecord.Substring(0, intIndex))
                strEmpRecord = strEmpRecord.Remove(0, intIndex + 1)

                intIndex = strEmpRecord.IndexOf(",")
                strDate(intCount) = strEmpRecord.Substring(0, intIndex)
                strEmpRecord = strEmpRecord.Remove(0, intIndex + 1)

                intIndex = strEmpRecord.IndexOf(",")
                strCountyState(intCount) = strEmpRecord.Substring(0, intIndex)
                strEmpRecord = strEmpRecord.Remove(0, intIndex + 1)

                intIndex = strEmpRecord.IndexOf(",")
                strRace(intCount) = strEmpRecord.Substring(0, intIndex)
                strEmpRecord = strEmpRecord.Remove(0, intIndex + 1)

                intIndex = strEmpRecord.IndexOf(",")
                intNumberInHousehold(intCount) = strEmpRecord.Substring(0, intIndex)
                strEmpRecord = strEmpRecord.Remove(0, intIndex + 1)

                intIndex = strEmpRecord.IndexOf(",")
                dblYearlyIncome(intCount) = CDbl(strEmpRecord.Substring(0, intIndex))
                strEmpRecord = strEmpRecord.Remove(0, intIndex + 1)



                'Add data collected to list box
                For intIndex = 0 To intRand.Length - 1
                    Me.lstData.Items.Add("ID Code: " & vbTab & vbTab & vbTab & intRand(intIndex))
                Next
                For intIndex = 0 To strDate.Length - 1
                    Me.lstData.Items.Add("Date: " & vbTab & vbTab & vbTab & strDate(intIndex))
                Next
                For intIndex = 0 To strCountyState.Length - 1
                    Me.lstData.Items.Add("County: " & vbTab & vbTab & vbTab & strCountyState(intIndex))
                Next
                For intIndex = 0 To strRace.Length - 1
                    Me.lstData.Items.Add("Race: " & vbTab & vbTab & vbTab & strRace(intIndex))
                Next
                For intIndex = 0 To intNumberInHousehold.Length - 1
                    Me.lstData.Items.Add("Number in Household: " & vbTab & intNumberInHousehold(intIndex))
                    Me.lstData.Items.Add("")
                Next
                'For intIndex = 0 To dblYearlyIncome.Length - 1
                '    Me.lstData.Items.Add("Yearly Income: " & vbTab & dblYearlyIncome(intIndex))
                'Next


            Loop

            'Close the file
            EmpFile.Close()
        Else
            MessageBox.Show("File does not exist")

        End If


    End Sub



This post has been edited by oldnewbie: 17 January 2012 - 08:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Array questions

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Array questions

Posted 17 January 2012 - 08:46 PM

Quote

I'm using Streamwriter/Streamreader for the first time in this project and keep getting an "Argument out of Range Exception was unhandled" error. It says "length cannot be less than zero. Parameter name: length." This happens at "dblYearlyIncome."
was this error happening to this current code or was happening before? At which line exactly? Also did you any changes since you last get that error?

And yes you can loop through your array and count the occurrence of each element.
Yes the count of array + 1 means hw many elements we have.
Was This Post Helpful? 1
  • +
  • -

#3 oldnewbie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 16-December 11

Re: Array questions

Posted 17 January 2012 - 08:59 PM

It happens at line 59. I suspect there is a problem with the dbl data type. If I comment out that code everything else works fine.

Is this how I would loop the array to find the total count of the array? Does it work the same way with finding the selected value of a combobox? Thanks.

For intIndex = 0 To intNumberInHousehold.Length - 1
                    frmSurveyAll.lblTotalHouseholdSurveyed.Text = CStr(intNumberInHousehold(intIndex))
                    intCount = +1
                Next


Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Array questions

Posted 18 January 2012 - 05:45 AM

You dont need even to loop, because you have the Length of the array which is enough for you as I said earlier!


To know the selected value, we have a property of combobox selectedItem which will give you the selected item of the combobox.

About your error at line 59 which is in your code:\
dblYearlyIncome(intCount) = CDbl(strEmpRecord.Substring(0, intIndex))

I dont see the way to happen the error length cannot be less than zero. Parameter name: length. Are you sure this is the line? or any error message now? Because here we have index not length, may be the error to happen when we Redim
Was This Post Helpful? 1
  • +
  • -

#5 oldnewbie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 16-December 11

Re: Array questions

Posted 18 January 2012 - 06:17 AM

Thank you for the information regarding getting the count of the arrays. And yes, I'm sure this error is happening at line 59. Like I said, if I comment out that statement I don't get the error. Weird.
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Array questions

Posted 18 January 2012 - 06:38 AM

A little advice, the part of the code after comment
'Add data collected to list box
better be done outside the do loop. This is because there is repeatition of the same everytime, but you can put that after the do loop and it will be added only once!

Ok now I see, I was looking at the availability of error in the left hand side of the statement bu the error is coming from the right hand side of the statement. So the problem is as follows:
intIndex = strEmpRecord.IndexOf(",") 'try to find the index of ',' but it seems we have no anymore ',' so it returns -1
dblYearlyIncome(intCount) = CDbl(strEmpRecord.Substring(0, intIndex))' when you try here -1 will not be accepted.

The reason of the error is because there is a line or lines which has only 5 commas, so when you try to find the index of the sixth comma it returns -1.
To solve the problem review your text file to see how many commas each line has.
Was This Post Helpful? 1
  • +
  • -

#7 oldnewbie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 16-December 11

Re: Array questions

Posted 18 January 2012 - 07:14 AM

Ah, that was it! I took out the line of code on line 58 and it works now. Too many commas. Thank you! I will try later to do my calculations with the comboboxes.
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Array questions

Posted 18 January 2012 - 07:31 AM

Glad we could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1