2014-07-23 3 views
-1

오류 메시지이었다 :얻기 오류 : 필드 이니셜 비 정적 필드, 메소드 또는 속성을 참조 할 수

namespace AmazingPaintball 
{ 
class Paintball 
{ 
    public Point startPoint; 


    public Paintball(Point myPoint) 
    { 
     startPoint = myPoint; 


    } 

이이있다 :이 생성자

Error 1 A field initializer cannot reference the non-static field, method, or property 'AmazingPaintball.Form1.thePoint'  

Point thePoint = new Point(50, 50); 
    Paintball gun = new Paintball(thePoint); 
+4

어디에서 오류가 발생 했습니까? 그것은 방법에 있습니까? – clcto

답변

6

충분한 컨텍스트를 표시하지 않았습니다. b 컴파일러는 말한다

class Game 
{ 
    Point thePoint = new Point(50, 50); 
    Paintball gun = new Paintball(thePoint); 
} 

으로 필드 이니셜 라이저가 다른 필드 또는 인스턴스 멤버를 참조 할 수 없습니다 : 유타는 당신이 뭔가를 가지고 생각한다. 이 솔루션은 비록 간단합니다 - 생성자에서 초기화를 넣어 : 정말 당신을 가정 것

class Game 
{ 
    Point thePoint; 
    Paintball gun; 

    public Game() 
    { 
     thePoint = new Point(50, 50); 
     gun = new Paintball(thePoint); 
    } 
} 

두 필드가 필요 마음 당신을. 만 실제로 gun 필드가 필요하면, 당신은 사용할 수 있습니다

class Game 
{ 
    Paintball gun = new Paintball(new Point(50, 50)); 
} 

을 (AS를 제외하고, 난 강력하게 the로 시작하는 변수 이름에 대해 조언을 줄 접두사는 추가 정보를 추가하지 않습니다 ... 그것입니다. 그냥 잡음.)

+1

어쩌면 변수가 대화의 "thePoint"를 나타낼 수도 있습니다. –

+2

*.로 시작하는 변수 이름에 대해 강력히 권고드립니다. * 점을 받아들이는 메소드 'GetTo'가있을 수 있습니다. –

+0

도움 주셔서 감사합니다. – ChefBrohan

관련 문제