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

New Topic/Question
Reply



MultiQuote




|