简体   繁体   English

C#类型为'System.ArgumentOutOfRangeException'的未处理异常错误

[英]C# An unhandled exception of type 'System.ArgumentOutOfRangeException' Error

I am getting the error An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in RCPYC Membership Program.exe Additional information: Index was out of range. 我收到错误消息RCPYC Membership Program.exe中发生了'System.ArgumentOutOfRangeException'类型的未处理异常。其他信息:索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。 in the following code. 在以下代码中。

private void ApplyBtn_Click(object sender, EventArgs e)
    {
        int index = 0;
        bool loop = true;
        string text;
        List<string> Data = new List<string>();
        while (loop == true)
        {
            if (index == 17)             //If statment takes a boolean value and sets it to the correct radio button
            {
                if (BtOwnerRdio.Checked == true)
                { Data[index] = "true";}
                else if(SocialRdio.Checked == true)
                {
                    Data[index] = "false";
                }
            }
            else                        //Else statement finds the correct textbox and sets the data to its text value
            {
                if (index < 10)
                { text = "tb0" + index.ToString(); }
                else
                { text = "tb" + index.ToString(); }

                Control tb = Controls.Find(text, true).FirstOrDefault() as Control; //Finds control with the name of the text string

                if (index != 0) //Does not encrypt the ID value
                {

                    try
                    {
                        MessageBox.Show("Try");
                        Data[index] = P.encrypt(tb.Text);  //Sets the list value to the textbox text value      
//This is the line that causes the error
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Fail");   
                        throw;         **Error is thrown here**
                    }
                }
            }

            index = index + 1;  //Adds 1 to the index to move to the next loop
            if (index == Data.Count - 1) //***
            { loop = false; }   //Ends the loop if the Data list is filled
                }

The code is getting the text value from a textbox ( tb ) and adding it to a list ( Data ) but returns the error when a blank value is returned. 该代码从文本框( tb )获取文本值,并将其添加到列表( Data ),但返回空值时返回错误。 I've checked the P.encrypt method completes when a blank value is returned and the error occurs when the string is added to the list. 我检查了P.encrypt方法是否在返回空白值时完成,并且在将字符串添加到列表时发生了错误。 The index when the list is called is 1 and I have tried manually setting the list capacity to 30, however I still get the error. 调用列表时的索引为1,我尝试将列表容量手动设置为30,但是仍然出现错误。

MY understanding of the error is that the index is either too high or negative, however if I have manually set the list capacity to 30 how can 1 be too high or negative? 我对错误的理解是索引太高或太负,但是如果我手动将列表容量设置为30,那么1太高或太负怎么可能?

There are two miss usage in this code. 此代码中有两种用法。

  1. You are setting the Capacity . 您正在设置“ 容量” Capacity does not mean current element quantity, it means the maximum quantity of elements. 容量不表示当前元素数量,而是表示最大元素数量。
  2. List does not act like array do. 列表不像数组那样工作。

Change Data[index] = P.encrypt(tb.Text); 更改Data[index] = P.encrypt(tb.Text); for Data.Add(P.encrypt(tb.Text)); 用于Data.Add(P.encrypt(tb.Text));

I think you have some confusion about what the capacity of a list is. 我认为您对列表的容量有些困惑。 For a List<T> the [Capacity][1] is just the number of elements the list can hold before it needs to resize its internal data structure. 对于List<T>[Capacity][1]只是列表在需要调整其内部数据结构大小之前可以容纳的元素数。 Importantly this does not mean that the list has 30 entries, it means that while the size of the list is less than 30 it will not need to do any resizing. 重要的是,这并不意味着该列表包含 30个条目,这意味着虽然列表的大小小于30,但它不需要进行任何大小调整。 It will still start with 0 entries when first created. 首次创建时,它仍以0项开头。

I assume that you haven't actually put anything into the list yet so Data[1] will indeed be out of range. 我认为您实际上还没有放入列表中,因此Data[1]确实超出范围。 If you want 30 initialised slots then you either need to add 30 items to initialise your list or use something like an array which would have 30 items if you set its size to 30. 如果您想要30个初始化的插槽,则需要添加30个项来初始化列表,或者使用类似数组的东西(如果将其大小设置为30,则该数组可以包含30个项)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM