需要帮助查找索引超出范围异常消息的原因

时间:2012-07-19 21:07:30

标签: c#

指数超出范围。必须是非负数且小于集合的大小。 参数名称:index

我有WireObjectAnimation类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using DannyGeneral;

namespace AnimationEditor
{
    class WireObjectAnimation
    {
        private List<WireObjectCoordinates> wocl = new List<WireObjectCoordinates>();

        private WireObject wo1 = null;

        string name;
        int ndx;
        public WireObjectAnimation(string name,WireObject wo)
        {

            this.name = name;

            wo1 = wo;

            WireObjectCoordinates woc;
            woc = new WireObjectCoordinates(wo.woc);
            wocl.Add(woc);
            ndx = 0;

        }





        public void Save(string path , string fileName , PictureBox pb) 
        {
            int framesNumberX = 0;
            int framesNumberY = 0;
            string fn;
            string t = Path.GetFileNameWithoutExtension(this.name);
            if (File.Exists(path + "\\" + "DATABASE" + "\\" + fileName + "\\" + t + ".txt"))
            {
                try
                {
                    string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + fileName);
                    File.Delete(f);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not delete file from disk. Original error: " + ex.Message);
                }


                fn = path + "\\" + "DATABASE" + "\\" + t + "\\" + fileName;
            }
            else
            {
                fn = path + "\\" + "DATABASE" + "\\" + fileName + "\\" + this.name + ".txt";
            }
            OptionsFile setting_file = new OptionsFile(fn);
            setting_file.SetKey("File Name", fn);
            setting_file.SetKey("Object Name", fileName);
            setting_file.SetKey("Animation Name", this.name);
            setting_file.SetKey("picturebox.Width", pb.Width.ToString());
            setting_file.SetKey("picturebox.Height", pb.Height.ToString());

            string[] xFrames = new string[wocl.Count];
            string[] yFrames = new string[wocl.Count];

            string X="";
            string Y="";
            for (int i = 0; i < wocl.Count; i++)
            {
                X  = string.Format("Frame_X_{0} ", i + 1);
                Y  = string.Format("Frame_Y_{0} ", i + 1);
                framesNumberX ++;
                framesNumberY ++;
                for (int j = 0; j < wocl[i].Point_X.Count; j++)
                {
                    xFrames[i] += string.Format("{0},", wocl[i].Point_X[j]);
                    yFrames[i] += string.Format("{0},", wocl[i].Point_Y[j]);


                }

                string tt = xFrames[i].Trim(",".ToCharArray());
                string yy =  yFrames[i].Trim(",".ToCharArray());



                 setting_file.SetKey(X, tt);
                 setting_file.SetKey(Y, yy);

            }

            int resultX = framesNumberX / 2;
            int resultY = framesNumberY / 2;
            setting_file.SetKey("Number Of Frames X", resultX.ToString()); 
            setting_file.SetKey("Number Of Frames Y", resultY.ToString());







        }


        public void Load(string path,string fileName)
        {
            int numberofframesX = 0;
            int numberofframesY = 0;
            string framesX = "";
            string framesY = "";
            string X = "";
            string Y = "";
            string t = path + "\\" + fileName;
            OptionsFile setting_file = new OptionsFile(t);
            string XX = setting_file.GetKey("Number Of Frames X");
            string YY = setting_file.GetKey("Number Of Frames Y");
            numberofframesX = Convert.ToInt32(XX);
            numberofframesY = Convert.ToInt32(YY);


            for (int i = 1; i < numberofframesX ; i++)
            {
            X  = string.Format("Frame_X_{0} ", i);
            framesX = setting_file.GetKey(X);
            List<string> floatStrings = new List<string>(framesX.Split(new char[] { ',' }));
            List<float> test = floatStrings.Select(tempStr => (float)Convert.ToDouble(tempStr)).ToList(); 


                wo1.woc.Point_X = test;  

            }
            for (int i = 1; i < numberofframesY; i++)
            {
                Y = string.Format("Frame_Y_{0} ", i);
                framesY = setting_file.GetKey(Y);
                List<string> floatStrings = new List<string>(framesY.Split(new char[] { ',' }));
                List<float> test = floatStrings.Select(tempStr => (float)Convert.ToDouble(tempStr)).ToList();
                wo1.woc.Point_Y = test;
            }
        }


        public void SetFrame(int frameNumber, WireObjectCoordinates woc)
        {
            wocl[frameNumber].Set(woc);
        }

        public WireObjectCoordinates GetFrame(int frameNumber)
        {
            if (frameNumber > wocl.Count)
            {
                throw new Exception("not allowed!");
            }

            if (frameNumber == wocl.Count)
            {
                WireObjectCoordinates woc;
                woc = new WireObjectCoordinates(wocl[wocl.Count - 1]); 

                wocl.Add(woc);
                return woc;
            }
            else
            {

                return wocl[frameNumber];
            }


        }
    }
}

现在当我正在加载Load函数时,我看到它的加载点很好。 但后来我试图将trackBar栏向右移动,然后我得到例外。现在这个:wo1.woc.Point_X = test; woc有4个索引,在每个索引中,Point_X和Point_Y都用每个索引中的数字填充。

在这个类中,我有函数SetFrame和GetFrame,我在trackBar的Form1滚动事件中使用GetFrame:

private void trackBar1_Scroll(object sender, EventArgs e)
        {

            currentFrameIndex = trackBar1.Value;
            textBox1.Text = "Frame Number : " + trackBar1.Value;
            wireObject1.woc.Set(wireObjectAnimation1.GetFrame(currentFrameIndex)); 
            LoadPictureAt(trackBar1.Value, sender);

            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button8.Enabled = false;
            SaveFormPicutreBoxToBitMapIncludingDrawings();

            return;









        }

现在当我将trackBar向右移动一次时,它应该从Point_X和Point_Y绘制下一组数字,而不是将其转到WireObjectCoordinates类并抛出异常:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AnimationEditor
{
    class WireObjectCoordinates
    {
        public List<float> Point_X = new List<float>();
        public List<float> Point_Y = new List<float>();

        public WireObjectCoordinates()
        {
        }

        public WireObjectCoordinates(WireObjectCoordinates w)
        {
            Point_X.AddRange(w.Point_X);
            Point_Y.AddRange(w.Point_Y);
        }

        public void Set(WireObjectCoordinates w)
        {
            for (int i = 0; i < Point_X.Count; i++)
            {
                Point_X[i] = w.Point_X[i];
                Point_Y[i] = w.Point_Y[i];
            }
        }
    }
}

例外是在线:Point_X [i] = w.Point_X [i]; Point_X [i]现在包含从[0]到[3]的4个索引,每个索引包含一个类似332.0 333.0 334.0 335.0的数字 并且w.Point_X [i]现在只包含一个索引[0],并且该索引的编号为332.0

我只是不明白为什么异常就在这条线上。

我的想法是,当我将trackBar向右移动时,它应该从wo1.woc.Point_Y和wo1.woc.Point_X中绘制下一个坐标,但我想我在Load函数中做错了什么?我不知道为什么它会抛出异常,只有当我将trackBar向右移动一次时才会抛出异常。

2 个答案:

答案 0 :(得分:0)

试一试它技术上不会解决问题,但它不会崩溃,只是阅读错误

答案 1 :(得分:0)

您希望for循环如何运作?循环变量i0变为此实例的Point_X列表的计数。但是,如果另一个实例w具有较低计数的Point_X列表,或者Point_Y列表thisPoint_Y列表{{1}有,它会失败。

如果您发现wPoint_X.Count4仅为w.Point_X.Count,那么当1超过i时,表达式{{ 1}}将尝试读取不存在的列表元素。这引发了重复&#34;指数超出了范围&#34;。

但也许你明白这一点?