2014-11-11 3 views
0

내 WFA에 다각형을 그리하려고를 찾을 수 있지만은그리기 다각형 C# OOP는 배열

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 
    } 
} 
+1

분명히 당신은 변수가 방법으로 범위가 클래스 수준 – TaW

+1

에'driehoek'에서 curvePoints의 __declaration__을 이동해야합니다. 'blackPen'처럼 클래스 멤버로 선언하십시오. – SimpleVar

+0

'curvePoints'는'driehoek' 메소드의 지역 변수입니다. 이렇게 Teken 메소드에서는 접근 할 수 없습니다. –

답변

0

이의 definetly가 내 수업에서 "curvePoints"를 찾을 수 없습니다 확실히 거기에

그것은 거짓말이다! 배열이 클래스가 아닌 다른 메소드 (Teken 범위를 벗어남)에 있습니다.

+0

하지만 내가 거기에 그들을 배치 그들은 point1을 찾을 수 없습니다 , 2,3 –

+0

@ J.Dekkers 상수가 아니라 선언 할 수는 있지만 나중에 지정할 수 있습니다. – Mephy

+0

그럼에도 불구하고 어디에서 선언/할당 할 것인가를 관리 할 수 ​​없다. (프로그래밍에 꽤 익숙하다.) –

1

당신이 Pen하게 직후, 클래스의 새로운 Point[] 만들기 : 다음

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, 

    }; 
} 
+0

point1,2,3은 선언문으로 사용될 수 없다. curvePoints의 끝에서 –