2014-02-26 2 views
3

나는 C를 잘 쓰지 못하고 C#을 잘 못 쓰는 데 익숙하다.다른 클래스 간의 함수 호출

제 문제는 openAnotherForm()Welcome_Form에서 실행할 수 있고 지금 코드가 작동하지 않기를 바랍니다. 나는 참을성있게 다른 일을 시도했지만 오직 나의 좌절을 밀어 냈습니다.

문제를 설명하기 위해 관련 코드가 단순화되었습니다.

파일 1 -이 실행하고 파일 열기 2.

class UIcode 
{ 
    private Welcome_Form Welcome; 
    private AnotherForm_Form AnotherForm; 

    public UIcode() 
    { 
     Welcome = new Welcome_Form(); 
     Application.Run(Welcome); 
    } 

    public void openAnotherForm() 
    { 
     Welcome.Hide(); 
     AnotherForm = new AnotherForm_Form(); 
     AnotherForm.ShowDialog(); 
    } 
} 

파일 2 - 나는 TheButton을 클릭하면, 프로그램은 파일에서 기능 openAnotherFrom를 실행해야합니다 1.

public partial class Welcome_Form : Form 
{ 
    public Welcome_Form() 
    { 
     InitializeComponent(); 
    } 

    private void TheButton_Click(object sender, EventArgs e) 
    { 
     // Function from file 1 
     UIcode.openAnotherForm(); 
    } 
} 

내가 실현 문제는 아주 사소한 일이지만 나는 이것을 어떻게 수행하는지에 대한 설명에 대해 여전히 감사 할 것입니다.

선호 : UIcode의 함수는 UIcode가 지정한 클래스에서만 인식되어야합니다.

+0

'openAnotherForm'은 UICode의 인스턴스 메소드입니다.이 메소드는 인스턴스를 실행해야 할 필요가 있음을 의미합니다. 또는 메소드를 정적으로 만들어야합니다. 즉,이 경우 리팩터링이 필요하거나 작동하지 않는 UICode의 인스턴스 변수 나 메소드를 참조 할 수 없음을 의미합니다. – Chris

+0

두 번째 양식은 열어 본 부모에 대한 참조가 필요합니다. 생성자에서 전달할 수 있습니다. –

답변

3

당신은 그것을 열어 UIcode의 인스턴스에 대한 참조를 가지고 생성자를 변경할 수 있습니다 :

private void TheButton_Click(object sender, EventArgs e) 
    { 
     // Function from file 1 
     myParent.openAnotherForm(); 
    } 
+0

이것은 내가 바라는 바른 아이디어이며 이미 시도한 것과 비슷합니다.그러나'public Welcome_Form (UIcode parent) '에 오류가 발생합니다. – Daltons

+0

@ user3064934 : 어떤 오류가 있습니까? 한 노트, 공용 클래스로'UIcode'를 표시해야합니다. –

+0

예, 그랬습니다 !! 많이 고맙습니다 :) 이제는 대부분의 신청서에 대한 구조가 있습니다. – Daltons

0

openAnotherForm() 방법이 static이 아니므로 인스턴스 참조가 필요합니다. UICode 객체를 인스턴스화하거나 메서드를 static으로 표시하십시오.

0

File1에 클래스의 인스턴스를 만들어 메서드를 호출 할 수 있습니다. 클래스 UICode을 호출 했으므로 생성자의 이름을 public UserInterface()에서 public UICode()으로 변경해야합니다.

class UIcode 
{ 
    private Welcome_Form Welcome; 
    private AnotherForm_Form AnotherForm; 

    public UIcode() // Renamed Constructor 
    { 
     Welcome = new Welcome_Form(); 
     Application.Run(Welcome); 
    } 

    public void openAnotherForm() 
    { 
     Welcome.Hide(); 
     AnotherForm = new AnotherForm_Form(); 
     AnotherForm.ShowDialog(); 
    } 
} 

public partial class Welcome_Form : Form 
{ 
    public Welcome_Form() 
    { 
     InitializeComponent(); 
    } 

    private void TheButton_Click(object sender, EventArgs e) 
    { 
     // Create an instance UICode 
     UICode instance = new UICode(); 

     // Call the method from the instance, not from the class. 
     instance.openAnotherForm(); 
    } 
} 

또는, openAnotherForm()static 방법을 만들 수 있습니다,하지만 당신은 또한 인스턴스 변수 (WelcomeAnotherForm) static를 확인해야합니다. 초기화 할 필요가 있지만 생성자를 static으로 만들 수 있습니다. UIcode에서 이제

private static UIcode myParent; 

    public Welcome_Form(UIcode parent) 
    { 
     myParent = parent; 
     InitializeComponent(); 
    } 

: Welcome_Form 다시 마지막

public UIcode() 
    { 
     Welcome = new Welcome_Form(this); 
     Application.Run(Welcome); 
    } 

그리고,

class UIcode 
{ 
    private static Welcome_Form Welcome; 
    private static AnotherForm_Form AnotherForm; 

    public static UIcode() // Renamed Constructor 
    { 
     Welcome = new Welcome_Form(); 
     Application.Run(Welcome); 
    } 

    public static void openAnotherForm() 
    { 
     Welcome.Hide(); 
     AnotherForm = new AnotherForm_Form(); 
     AnotherForm.ShowDialog(); 
    } 
} 
+0

이것은 인스턴스 변수를 참조하기 때문에 작동하지 않습니다. – Chris

+0

사실, 고칠 것입니다. 고마워요. – elnigno

+0

@ user3064934 : elnigno가 말했듯이 그것은 인스턴스 멤버를 참조하기 때문입니다. 한 가지 질문은 'UIcode'가 모두 정적이 아닌 이유가 무엇입니까? 한 가지 해결책은 바로 그렇게하는 것입니다. –