C# 需要帮助查找索引超出范围异常消息的原因吗
索引超出范围。必须为非负数且小于集合的大小。 参数名称:索引 我的课程是WireObjectAnimation:C# 需要帮助查找索引超出范围异常消息的原因吗,c#,C#,索引超出范围。必须为非负数且小于集合的大小。 参数名称:索引 我的课程是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 Wir
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];
}
}
}
}
现在,当我向右移动轨迹栏一次时,它应该从点X和点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];
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间动画编辑器
{
类WireObjectCoordinates
{
公共列表点_X=新列表();
公共列表点_Y=新列表();
公共WireObjectCoordinates()
{
}
公共WireObjectCoordinates(WireObjectCoordinates w)
{
点X添加范围(w点X);
点Y添加范围(w点Y);
}
公共无效集(WIREOBJECTW)
{
对于(int i=0;i
例外情况在这一行:Point_X[i]=w.Point_X[i];
点_X[i]现在包含从[0]到[3]的4个索引,每个索引包含一个类似332.0 333.0 334.0 335.0的数字
现在w.Point_X[i]只包含一个索引[0],这个索引的编号为332.0
我只是不明白为什么这条线上有例外
想法是,当我向右移动轨迹栏时,它应该从wo1.woc.Point_Y和wo1.woc.Point_X绘制下一个坐标,但我想我在加载函数中做错了什么?我不知道它为什么会抛出异常,而且只有当我将轨迹栏向右移动一次时才会抛出异常。在它周围抛出一个try catch。从技术上讲,它不会解决问题,但它不会再崩溃,只需重新阅读错误即可。您希望
for
循环如何工作?循环变量i
从0
到该实例的点X
列表的计数。但是,如果另一个实例w
有一个计数较低的点X
列表,或者如果点Y
这个列表或点Y
列表的点w
有,它将失败
如果您意识到Point\u X.Count
是4
而w.Point\u X.Count
只是1
,那么当i
超过0
时,表达式w.Point\u X[i]
将尝试读取列表中不存在的元素。这就抛出了“指数超出范围”的理由
但也许你明白这一点?-1为GiantWallO的代码。这里缺少项目文件!
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];
}
}
}
}