开发者

Multiple embeded array of object errors in a class constructor

开发者 https://www.devze.com 2023-02-19 19:04 出处:网络
There\'s two classes: Point and Triangle. Point is by itself, and Triangle should have an array of 3 embedded objects of class Point.

There's two classes: Point and Triangle. Point is by itself, and Triangle should have an array of 3 embedded objects of class Point.

Point works fine, but when it came to creating Triangle a bunch of errors appeared. Where hath I went wrong?

Point:

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

namespace Classes
{
    class Point
    {
        private int x;
        private int y;

        public int getX()
        {
            return x;
        }
        public int getY()
        {
            return y;
        }

        public void setX()
        {
            //get user input, validate
            Console.WriteLine("Enter X co-ord: ");
            int inputX;
            if (Int32.TryParse(Console.ReadLine(), out inputX))
            {
                setX(inputX);
            }
            else
            {
                Console.WriteLine("Invalid input value");
                Console.WriteLine("Assigning default value of 0 to X co-ord");
                setX(0);
            }
        }
        public void setX(int xx)
        {
            x = xx;
        }
        public void setX(Point px)
        {
            if (px == null)
                x = 0;
            else
                x = px.x;
        }
        public void setY()
        {
            //get user input, validate
            Console.WriteLine("Enter Y co-ord: ");
            int inputY;
            if (Int32.TryParse(Console.ReadLine(), out inputY))
            {
                setY(inputY);
            }
            else
            {
                Console.WriteLine("Invalid input value");
                Console.WriteLine("Assigning default value of 0 to Y co-ord");
                setY(0);
            }
        }
        public void setY(int yy)
        {
            y = yy;
        }
        public void setY(Point py)
        {
            if (py == null)
                y = 0;
            else
                y = py.y;
        }

        public void setPoint()
        {
            setX();
            setY();
        }
        public void setPoint(int xx, int yy)
        {
            setX(xx);
            setY(yy);
        }

        public void setPoint(Point p)
        {
            setX(p);
            setY(p);
        }

        public Point()
        {
            setPoint();
        }

        public Point(int xx, int yy)
        {
            setPoint(xx, yy);
        }

        public Point(Point p)
        {
            setPoint(p);
        }

        public static Point operator +(Point p1, Point p2)
        {
            Point temp = new Point(0,0);
            temp.x = p1.getX() + p2.getX();
            temp.y = p1.getY() + p2.getY();
            return temp;
        }

        public static Point operator -(Point p1, Point p2)
        {
            Point temp = new Point(0, 0);
            temp.x = p1.getX() - p2.getX();
            temp.y = p1.getY() - p2.getY();
            return temp;
        }

        public static Point Add(Point p1, Point p2)
        {
            Point temp = new Point(0, 0);
            temp.x = p1.getX() + p2.getX();
            temp.y = p1.getY() + p2.getY();
            return temp;
        }

        public static Point Add(int xx, int yy ,Point p)
        {
            Point temp = new Point(0, 0);
            temp.x = xx + p.getX();
            temp.y = yy + p.getY();
            return temp;
        }

        public static Point Add(int x1, int x2, int y1, int y2)
        {
            Point temp = new Point(0, 0);
            temp.x = x1 + x2;
            temp.y = y1 + y2;
            return temp;
        }

        public Point Add(Point p)
        {
            Point temp = new Point(0, 0);
            temp.x = getX() + p.x;
            temp.y = getY() + p.y;
            return temp;
        }

        public Point Add(int xx, int yy)
        {
            Point temp = new Point(0, 0);
            temp.x = getX() + xx;
            temp.y = getY() + yy;
            return temp;
        }

        public static Point Subtract(Point p1, Point p2)
        {
            Point temp = new Point(0, 0);
            temp.x = p1.getX() - p2.getX();
            temp.y = p1.getY() - p2.getY();
            return temp;
        }

        public static Point Subtract(int xx, int yy, Point p)
        {
            Point temp = new Point(0, 0);
            temp.x = xx - p.getX();
            temp.y = yy - p.getY();
            return temp;
        }

        public static Point Subtract(int x1, int x2, int y1, int y2)
        {
            Point temp = new Point(0, 0);
            temp.x = x1 - x2;
            temp.y = y1 - y2;
            return temp;
        }

        public Point Subtract(Point p)
        {
            Point temp = new Point(0, 0);
            temp.x = getX() - p.x;
            temp.y = getY() - p.y;
            return temp;
        }

        public Point Subtract(int xx, int yy)
        {
            Point temp = new Point(0, 0);
            temp.x = getX() - xx;
            temp.y = getY() - yy;
            return temp;
        }

        public void displayPoint()
        {
            System.Console.WriteLine("Point: X={0} ; Y={1}", getX(), getY());
        }

        public bool isEqual(Point p)
        {
            if (x == p.x && y == p.y)
                return true;
            else
                return false;
        }

        public static bool isEqual(Point p1, Point p2)
        {
            if (p1.x == p2.x && p1.y == p2开发者_如何学JAVA.y)
                return true;
            else
                return false;
        }

        public bool isNotEqual(Point p)
        {
            if (this.isEqual(p) == true)
                return false;
            else
                return true;
        }

        public static bool isNotEqual(Point p1, Point p2)
        {
            if (Point.isEqual(p1, p2) == true)
                return true;
            else
                return false;
        }

        public static bool operator ==(Point p1, Point p2)
        {
            if (p1.isEqual(p2) == true)
                return true;
            else
                return false;
        }

        public static bool operator !=(Point p1, Point p2)
        {
            if (p1.x == p2.x && p1.y == p2.y)
                return true;
            else
                return false;
        }
    }

}

Triangle:

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

namespace Classes
{
    class Triangle
    {
        private Point pnt[]; /*Bad array declarator*/

        public Triangle()
        {
            pnt = new Point[3]; /*Can't convert Point[] to Point*/
            for (int i=0; i<pnt.Length; i++) /*Length doesn't exists in Point*/
                pnt[i] = new Point(); /*Can't apply indexing to Point[]*/
        }
    }
}


In C# it is:

private Point[] pnt;


You can't declare a variable of an array type like this:

private Point pnt[];

It has to be like this:

private Point[] pnt;

That's also the preferred way in Java (which is the language I suspect you're more familiar with), although Java allows you the former version too. It makes more sense in my view, because it keeps all the type information in one place, separate from the variable name.

The next thing you should probably look at is how properties are specified in C# - using getX and setX makes it look more like Java than C#.


Point[] pnt;
public Triangle()
{
    pnt = new Point[3];
    for (int i = 0; i < pnt.Length; ++i) pnt[i] = new Point();
}

Also, System.Drawing already has a Point structure that you may want to use.


First error - you don't declare arrays that way.

private Point[] pnt;

new Point[3] creates an array of Point with 3 items. You can't assign this array to an object of type Point (which at this point is what pnt is.

Since pnt is a Point object and not an array, it will not have a Length property. This is also why you can't apply indexing (pnt[i]) to it.


All of the other errors follow from the first one - incorrect array declaration.


I'll try to explain the error messages:

private Point pnt[]; /*Bad array declarator*/

The array declarator which is "[]" must stand right behind the type and not behind the variable so change the line to private Point[] pnt;

pnt = new Point[3]; /*Can't convert Point[] to Point*/
for (int i=0; i<pnt.Length; i++) /*Length doesn't exists in Point*/
    pnt[i] = new Point(); /*Can't apply indexing to Point[]*/

The following message will disappear after correcting the array declaration. In your code above the type of pnt is still Point and so accessing Array methods or indexes will fail.

0

精彩评论

暂无评论...
验证码 换一张
取 消