Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
1.94/5 (3 votes)
See more:
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
C#
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
Posted
Comments
Sergey Alexandrovich Kryukov 25-Sep-14 3:04am    
Error is never "unknown". What is the problem? What did you want to achieve, how? What happened instead and why do you feel this result was wrong?... and so on...
—SA
Engineer khalid 25-Sep-14 3:32am    
the program compile with no error but at runtime the debugger wrote "use the new keyword to create an object instance"
i want to achieve storing data of those 2 lines using the 2 class,i fell this result is wrong because i feel i might need a property for an array of points in class data_line frankly i do not know how to do that
in general the program does not works even though it compile fine

Setting NPOINT to 2 doesn't allocate the array to store the points.
So when you do this;
C#
Myline[0].NPOINT = 2;

the backing field pnt of data_line is not changed to cater for storing 2 items.

Change
C#
public int NPOINT{  get{return Npoint;}  set{ Npoint = value; }    }

to
public int NPOINT{  get{return Npoint;}  set { Npoint = value; pnt = new point[value]; } }


(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 points instead of an array, it'll be easier to add/remove items then I think.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Engineer khalid 25-Sep-14 3:40am    
i have changed the property of NPOINT still does not works,would u please write to me how to do that using List of points.
Fredrik Bornander 25-Sep-14 3:48am    
What error are you getting?
Engineer khalid 25-Sep-14 4:08am    
Object reference not set to an instance of an object.
use the new keyword to create an object instance
note the following line is heighlighted
Myline[0].pnt[0].X = 1;//located in FormLoad above
Fredrik Bornander 25-Sep-14 4:19am    
And what does your data_line class look like now?
Engineer khalid 25-Sep-14 4:29am    
it works when i added this code to formload
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();
it works after adding codes in Formload

C#
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;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900