Index was outside the bounds of the array. HELP

  • Thread starter Thread starter Godren
  • Start date Start date
G

Godren

Guest
Public Class frmMain
Dim D1835, R1835, I1835 As Integer

Dim D3554, R3554, I3554 As Integer

Dim D54Over, R54Over, I54Over As Integer

Dim DTotal, RTotal, ITotal As Integer
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
lstParty.Items.Add("Democrates")

lstParty.Items.Add("Republicants")

lstParty.Items.Add("Independence")

D1835 = 0

D3554 = 0

D54Over = 0

DTotal = 0

I1835 = 0

I3554 = 0

I54Over = 0

ITotal = 0

R1835 = 0

R3554 = 0

R54Over = 0

RTotal = 0
End Sub

Private Sub btnWrite_Click(sender As Object, e As EventArgs) Handles btnWrite.Click
Dim OutFileName As String = "C:\Users\KEK\source\repos\PAO Solution\PAO Project\bin\Debug\PAO.txt"

'Condition to check whether file exists or not.

If System.IO.File.Exists(OutFileName) = True Then

Using OutFileWriter As New System.IO.StreamWriter(OutFileName, True)

OutFileWriter.Flush()

If (txtAge.Text) >= 18 Then

If (lstParty.SelectedIndex = 0) Then

'Write Democrats count.

OutFileWriter.WriteLine("Democrats:" + txtAge.Text)

OutFileWriter.Close()

'Display message.

MsgBox("Saved in text file")

ElseIf (lstParty.SelectedIndex = 1) Then

'Write Republicans count.

OutFileWriter.WriteLine("Republicans:" + txtAge.Text)

OutFileWriter.Close()

'Display message.

MsgBox("Saved in text file")

ElseIf (lstParty.SelectedIndex = 2) Then

'Write Independents count.

OutFileWriter.WriteLine("Independents:" + txtAge.Text)

OutFileWriter.Close()

'Display message.

MsgBox("Saved in text file")

End If

Else

MsgBox("Age not valid")

End If

End Using

'Otherwise.

Else

'Display message.

MsgBox("File not Exist")

End If

'End of button one click.
End Sub

Private Sub btnDisplayTotal_Click(sender As Object, e As EventArgs) Handles btnDisplayTotal.Click
Dim TextLine As String
' Open file.
FileOpen(1, "C:\Users\KEK\source\repos\PAO Solution\PAO Project\bin\Debug\PAO.txt", OpenMode.Input)

' Loop until end of file.
Do Until EOF(1)

' Read the line into a variable.

TextLine = LineInput(1)

Dim delimiter As Char = ":"
Dim substrings() As String = TextLine.Split(delimiter)

If (substrings(0) = "Democrats") Then

If (substrings(1) >= 18 And substrings(2) < 35) Then

D1835 = D1835 + 1

DTotal = DTotal + D1835

ElseIf (substrings(1) >= 36 And substrings(1) <= 54) Then

D3554 = D3554 + 1

DTotal = DTotal + D1835

ElseIf (substrings(1) >= 54) Then

D54Over = D54Over + 1

DTotal = DTotal + D1835

End If

ElseIf (substrings(0) = "Republicans") Then

If (substrings(1) >= 18 And substrings(1) <= 35) Then

R1835 = R1835 + 1

RTotal = RTotal + R1835

ElseIf (substrings(1) >= 36 And substrings(1) <= 54) Then

R3554 = R3554 + 1

RTotal = RTotal + R1835

ElseIf (substrings(1) >= 54) Then

R54Over = R54Over + 1

RTotal = RTotal + R1835

End If

ElseIf (substrings(0) = "Independents") Then

If (substrings(1) >= 18 And substrings(1) <= 35) Then

I1835 = I1835 + 1

ITotal = ITotal + I1835

ElseIf (substrings(1) >= 36 And substrings(1) <= 54) Then

I3554 = I3554 + 1

ITotal = ITotal + I1835

ElseIf (substrings(1) >= 54) Then

I54Over = I54Over + 1

ITotal = ITotal + I1835

End If

End If

Loop

FileClose(1)

txtD1835.Text = D1835.ToString()
txtD3554.Text = D3554.ToString()
txtDOver54.Text = D54Over.ToString()
txtI1835.Text = I1835.ToString()
txtI3554.Text = I3554.ToString()
txtIOver54.Text = I54Over.ToString()
txtR1835.Text = R1835.ToString()
txtR3554.Text = R3554.ToString()
txtROver54.Text = R54Over.ToString()
txtDTotal.Text = (D1835 + D3554 + D54Over).ToString()
txtRTotal.Text = (R1835 + R3554 + R54Over).ToString()
txtITotal.Text = (I1835 + I3554 + I54Over).ToString()
End Sub
End Class




Working on this assignment, But I keep getting "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" for this line:

If (substrings(1) >= 18 And substrings(2) < 35) Then
Anyone know what's causing this?

Continue reading...
 
Back
Top