MyArmsFellOff MyArmsFellOff - 3 months ago 10
Vb.net Question

Errors occuring when using list

I have a list that I have entered five items into using this code:

If (surname.Count + 1) < noofcompetitors Then
surname.Add(txtSurname.Text)
Else....


I am attempting to see these items using this code:

Console.WriteLine(surname.Item(0))
Console.WriteLine(surname.Item(1))
Console.WriteLine(surname.Item(2))
Console.WriteLine(surname.Item(3))
Console.WriteLine(surname.Item(4))


However, I get the error 'System.ArgumentOutOfRangeException'. I'm not sure what's causing this, so any help would be appreciated.

Answer

Why not just use a for each loop?

    For Each name As String In surname
        Console.WriteLine(name)
    Next

That way you won't get the exception and you can see how many names are actually being added to your list

Comments