使用列表时发生的错误 [英] Errors occurring when using list

查看:25
本文介绍了使用列表时发生的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,我使用此代码输入了五个项目:

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))  

但是,我收到错误System.ArgumentOutOfRangeException".

However, I get the error 'System.ArgumentOutOfRangeException'.

推荐答案

为什么不使用 for each 循环?

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

这篇关于使用列表时发生的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆