I'm trying to switch "M" to "Mr." and "F" to "Ms." when the last name is selected in the listbox. When I clicked on the first name it worked, but when I clicked on anyother name, I got this error message:

--Additional information: Index was outside the bounds of the array.--

The information in the text file is like this:

   Ball,Krystal,F,1981
   Banks,Robin,F,1988
   Burgher,Hamilton,M,1980
   Early,Brighton,M,1989
   Hedd,MT,M,1960
   Hogg,Ima,F,1953
   Knapp,Anita,F,1970
   Overnout,Roger,M,1968
   Psito,Arnie,M,1962
   Teak,Anne,F,1939

And my code is as follows:

Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
Dim names As IO.StreamReader = IO.File.OpenText("Info.txt")
Dim lName As String = lstNames.Text
Dim line As String
Dim gender As String
Dim foundFlag As Boolean = False
Do Until foundFlag Or names.EndOfStream
    line = names.ReadLine
    If line.Split(","c)(2) = "M" Then
        gender = "Mr. "
    ElseIf line.Split(","c)(2) = "F" Then
        gender = "Ms. "
    End If
    If line.Split(","c)(0) = lName Then
        txtOutput.Text = gender & line.Split(","c)(1) & " " & line.Split(","c)(0) & " is " & 2012 - line.Split(","c)(3)
        foundFlag = True
    End If
Loop
End Sub

I'm trying to display e.g"Ms. Krystal Ball is 31" etc. well the last name is selected in the listbox. Can someone please let me know what's wrong. Thanks in advance.

This question has already been answered. Start a new discussion instead.