课堂上的未知错误 [英] unknown error in class
问题描述
在我的项目中我要保存2行数据,0行有2点而第1行有3点
i写了2级第1类(点)保持每行的点数另一个类(data_line)是保持每行的点数(Npoint)
i必须在类data_line中的某处出现错误,帮助
Hellow in my project i want to save data of 2 lines,line zero has 2 points while line one has 3 points
i wrote 2 class the 1st class (point) to keep points of each line the other class (data_line) is to keep numbers of points(Npoint) of each line
i must have an error somewhere in class data_line ,help
namespace PreWinf
{
class point
{
private double x,y,z;
public point() { }
public point(double xi, double yi, double zi)
{ X = xi; Y = yi; Z = zi; }
public double X{ get{return x;} set{x=value;} }
public double Y{ get{return y;} set{y=value;} }
public double Z{ get{return z;} set{z=value;} }
}
}
namespace PreWinf
{
class data_line
{
private int Npoint;
//public point[] pnt=new point[100];
public point[] pnt;
public data_line() { }
public data_line(int XNpoint)
{ Npoint = XNpoint; }
public point this[int i]
{ get { return pnt[i]; } set { pnt[i] = value; } }
//
public int NPOINT{ get{return Npoint;} set{ Npoint = value; } }
}
}
private void FormLoad(object sender, EventArgs e)
{int i;
data_line[] Myline = new data_line[100];
for (i = 0; i < 100; i++)
Myline[i] = new data_line();
Myline[0].NPOINT = 2;//line zero has 2 points
Myline[0].pnt[0].X = 1; Myline[0].pnt[0].Y = 1; Myline[0].pnt[0].Z = 1;
Myline[0].pnt[1].X = 2; Myline[0].pnt[1].Y = 2; Myline[0].pnt[1].Z = 2;
Myline[1].NPOINT = 3;//line 1 has 3 points
Myline[1].pnt[0].X = 1; Myline[1].pnt[0].Y = 2; Myline[1].pnt[0].Z = 3;
Myline[1].pnt[1].X = 4; Myline[1].pnt[1].Y = 5; Myline[1].pnt[1].Z = 6;
Myline[1].pnt[2].X = 7; Myline[1].pnt[2].Y = 8; Myline[1].pnt[2].Z = 9;
}//FormLoad
推荐答案
将NPOINT
设置为2
不分配数组来存储积分。
所以当你这样做时;
SettingNPOINT
to2
doesn't allocate the array to store the points.
So when you do this;
Myline[0].NPOINT = 2;
支持字段 pnt
data_line
的更改不适用于存储 2
项目。
更改
the backing field pnt
of data_line
is not changed to cater for storing 2
items.
Change
public int NPOINT{ get{return Npoint;} set{ Npoint = value; } }
to
to
public int NPOINT{ get{return Npoint;} set { Npoint = value; pnt = new point[value]; } }
(请注意,我已经遗漏了任何错误处理和调整大小或点数列表)。
在附注中,我会尝试使用点
点
s而不是数组,我认为添加/删除项目会更容易。
希望这会有所帮助,
Fredrik
(Note that I've left out any error handling and resizing or the points list from this).
On a side note, I would try using a List
of point
s instead of an array, it'll be easier to add/remove items then I think.
Hope this helps,
Fredrik
在Formload中添加代码后它可以工作
it works after adding codes in Formload
data_line[] Myline;//declared globally
private void FormLoad(object sender, EventArgs e)
{int i,j;
Myline = new data_line[100];
for (i = 0; i < 100; i++)
Myline[i] = new data_line();
for (i = 0; i < 100; i++)
Myline[i].pnt = new point[100];
for (i = 0; i < 100; i++)
for (j = 0; j <100; j++)
Myline[i].pnt[j] = new point();
Myline[0].NPOINT = 2;//line zero has 2 points
Myline[0].pnt[0].X = 1; Myline[0].pnt[0].Y = 1; Myline[0].pnt[0].Z = 1;
Myline[0].pnt[1].X = 2; Myline[0].pnt[1].Y = 2; Myline[0].pnt[1].Z = 2;
Myline[1].NPOINT = 3;//line 1 has 3 points
Myline[1].pnt[0].X = 1; Myline[1].pnt[0].Y = 2; Myline[1].pnt[0].Z = 3;
Myline[1].pnt[1].X = 4; Myline[1].pnt[1].Y = 5; Myline[1].pnt[1].Z = 6;
Myline[1].pnt[2].X = 7; Myline[1].pnt[2].Y = 8; Myline[1].pnt[2].Z = 9;
}
这篇关于课堂上的未知错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!