2012-07-19 40 views
-1

索引超出范围。必须是非负数且小于集合的大小。 参数名:指数需要帮助找到索引超出范围的异常消息的原因

我有类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]; 
      } 


     } 
    } 
} 

现在,当即时通讯做加载负载功能,我看到点它的加载它好。 但后来我试图移动trackBar栏滚动到右侧,然后即时获取异常。现在这个:wo1.woc.Point_X = test; woc有4个索引,每个索引中的Point_X和Point_Y都填充了数字。在跟踪条的Form1的滚动事件中使用的getFrame

在这个类中,我有功能SETFRAME和的getFrame和IM:

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; 









     } 

现在,当IM一旦移动跟踪条向右应该画下一组从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函数中做了错误的操作?我不知道为什么它抛出异常,它只有当我将轨道栏向右移动一次。

+6

-1。 – Puppy 2012-07-19 21:09:42

+1

这里没有项目文件! – 2012-07-19 21:17:16

回答

0

你怎么能指望你for循环工作?循环变量i0变为该实例的Point_X列表的计数。但如果其他实例w具有较低计数的Point_X列表,或者如果Point_Y列表thisPoint_Y列表w具有,它将会失败。

如果你意识到Point_X.Count4w.Point_X.Count只有1,那么当i超过0,表达w.Point_X[i]会尝试读取一个不存在的列表的元素。这引发了“索引超出范围”的说法。

但也许你明白吗?用于GiantWallO'Code的

0

扔它周围的尝试捕捉它在技术上不会解决这个问题,但它不会崩溃了,只是读了错误

相关问题