List< Label>投掷指数超出范围.必须为非负数并且小于集合的大小.错误 [英] List<Label> throwing Index was out of range. Must be non-negative and less than the size of the collection. error

查看:68
本文介绍了List< Label>投掷指数超出范围.必须为非负数并且小于集合的大小.错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

讨厌问这个,但是我要去哪里错了?

Hate to ask this but where am I going wrong?

public void foo(object sender, EventArgs e)
{
    List<Label> exerciseLabels = new List<Label>();

    string tempExercise = exerciseTextBox.Text;
    int numExercises = 0;
    day[tempDay].addExercise(numExercises, tempExercise);

    exerciseLabels.Add(new Label());
    exerciseLabels[1].Text = string.Format("{0}. {1}", numExercises + 1, tempExercise);

}

我收到的论点超出范围.

I am receiving an argument is out range.

谢谢.

推荐答案

List<Label> exerciseLabels = new List<Label>();
...
exerciseLabels.Add(new Label());
exerciseLabels[1].Text = ...

列表从0开始;第一项应为exerciseLabels[0].还是更好:

lists are 0-based; should be exerciseLabels[0] for the first item. Or better still:

var label = new Label();
label.Text = ...
exerciseLabels.Add(label);

无需访问索引器.

这篇关于List&lt; Label&gt;投掷指数超出范围.必须为非负数并且小于集合的大小.错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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