2

I am new to C# and I am struggling with this since hours and would appreciate your help.

I want to create a Polygon and Write down each position of the points.

Currently I have this: -Class Point

class Point
{
    private int x;
    private int y;


    public Point(int x2, int y2)
    {
        x = x2;
        y = y2;
    }
}

-Class Polygon

class Polygon
{
    private Point[] Points;

    public Polygon(params Point[] a)
    {
        Points = new Point[a.Length];
        for (int i = 0; i < a.Length; i++)
        {
            Points[i] = a[i];
        }
    }

    public Point this[int index]
    {
        get { return Points[index]; }
        set { Points[index] = value;}
    }
}

Now I have this in my main:

        Polygon First= new Polygon(new Point(7,4), new Point(4,1), new Point(2, 1));

        First[0] = new Point(3, 4);

        Console.WriteLine("points of polygon ");
        for (int i = 0; i < First.PointCounter; i++)
        {
            Console.WriteLine(First[i]);
        }

But now instead of seeing each position of the Point after "points of polygon" I see this in my Console: https://i.stack.imgur.com/ENSHI.jpg

How it should look like: https://i.stack.imgur.com/lFOSs.jpg

How it should look like: https://i.stack.imgur.com/lFOSs.jpg

carnito
  • 23
  • 3

2 Answers2

1

I added an override of ToString so that your Point class has the expected output when converted to string. An output like "x:3 y:4".

class Point
{
    public int x { get; private set; }
    public int y { get; private set; }

    public Point(int x2, int y2)
    {
        x = x2;
        y = y2;
    }

    public override string ToString()
    {
        return $"x:{x,-3} y:{y,-3}";
    }
}

As it is now, it is a good candidate for becoming a struct instead of class.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
0

C# is not "interpreted" like other languages, so the Console.WriteLine method won't guess what you're trying to have printed.

To give the result you're looking for, with your current code, you would have to provide public properties to your Point class:

public int X { get { return x;} set{ x = value;} }
public int Y { get { return y;} set{ y = value;} }

After which you could now access those properties in your for loop:

for (int i = 0; i < First.PointCounter; i++)
{
    Console.WriteLine($"x:{First[i].X}    y:{First[i].Y}");
}
SharpNip
  • 350
  • 3
  • 9
  • 1
    Provide the solution with .ToString() override as well that seems pretty good as well for this case. – Mahesh Apr 13 '19 at 02:19
  • @CoderofCode I'm personally not all that keen on overriding the `.ToString()` method. But if you would like to add it in here, do please go ahead. – SharpNip Apr 13 '19 at 02:20
  • `ToString()` has legitimate use cases. –  Apr 13 '19 at 02:54
  • 1
    @MickyD Absolutely. I'm just not keen on giving that as an answer to someone who's new to the language, as misuse of it can lead to pretty difficult debugging, from exp. But either way, Theodor Zoulias's solution works. – SharpNip Apr 13 '19 at 02:57
  • 1
    _"new to the language"_ - Ah yes excellent point. By giving the properties makes it more obvious what is happening. +1 good sir –  Apr 13 '19 at 03:02