绘制多边形C#OOP无法找到数组

时间:2014-11-11 12:03:33

标签: c# oop draw

我正在尝试在我的WFA中绘制一个Polygon,但它无法在我的课程中找到" curvePoints"它们肯定在那里

class Driehoek : Figuur
{
    Pen blackPen = new Pen(Color.Black, 3);

    public void driehoek(Point p)
    {
        //this.x = 120;
        //this.y = 50;
        //this.width = 100;
        //this.height = 100;

        Point point1 = new Point(100, 150);
        Point point2 = new Point(150, 100);
        Point point3 = new Point(200, 150);
        Point[] curvePoints =
         {
             point1,
             point2,
             point3,

         };



    }

    public override void Teken(Graphics g)
    {

        g.DrawPolygon(blackPen, curvePoints); 
        // Error here is: The name 'curvePoints' does not exist in the current context
    }
}

2 个答案:

答案 0 :(得分:1)

在您制作Point[]后立即在班级中创建新的Pen

class Driehoek
{
    Pen blackPen = new Pen(Color.Black, 3);
    Point[] curvePoints;
}

然后,稍微修改你的函数 ,以便为现有数组分配一个数组,而不是创建一个新数组:

public void driehoek(Point p)
{
    //this.x = 120;
    //this.y = 50;
    //this.width = 100;
    //this.height = 100;

    Point point1 = new Point(100, 150);
    Point point2 = new Point(150, 100);
    Point point3 = new Point(200, 150);

    //Changed Point[] curvePoints to just curvePoints
    curvePoints =
    {
        point1,
        point2,
        point3,

    };
}

答案 1 :(得分:0)

  

肯定在那里

这是谎言!你的数组是另一种方法(在“Teken”范围之外),而不是在课堂上。