2016-09-26 2 views
-1

위임자가 내 코드에서 작동하지 않는 이유를 알고있는 사람이 있습니까? 처음에는 양식을 표시하고 대리인을 사용하여 양식의 레이블을 업데이트하는 콘솔 응용 프로그램이 있습니다..Net 대리인이 작동하지 않습니다.

namespace DELEGATESAMPLEPROJECT 
{ 
    public class Program 
{ 
    public delegate void OnConfirmCall(); 
    public OnConfirmCall confirmCall; 

    [STAThread] 
    static void Main(string[] args) 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1(new Program().getReference())); 

     new Program().startFunctionCall(); 
    } 

    public Program getReference(){ 
     return this; 
    } 

    public void startFunctionCall(){ 
     Console.WriteLine("Function Call Started!"); 
     if(confirmCall != null){ 
      Console.WriteLine("Function Call Executing..."); 
      confirmCall(); 
     } 
    } 
} 
} 

FORM1

namespace DELEGATESAMPLEPROJECT 
{ 
    public partial class Form1 : Form 
    { 
     public Form1(Program thisProgramClass) 
     { 
      InitializeComponent(); 
      thisProgramClass.confirmCall += saySomething; 
     } 

     public void saySomething() 
     { 
      Label1.Text = "Hello World!"; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      Label1.Text = "Hi C#!"; 
     } 
    } 
} 

당신은 내가 "Hello World!"하지만 작동하지, 나는 무엇을 자사의 실종에 "Hi C#!"을 변경하려고 볼 수있다?

+6

두 개의 전혀 다른 'Program' 인스턴스를 사용하고 있기 때문입니다. 여기'Application.Run (새 Form1 (새 프로그램(). getReference())); '와'새로운 프로그램(). startFunctionCall();'을 사용하십시오. –

+0

@ChristophKn 대답으로 게시하십시오 – slawekwin

+0

@ ChristophKn - 프로그램 클래스의 이름을 변경해야합니까? – bernzkie

답변

2

글쎄, 당신은 Program 클래스의 두 인스턴스를 사용하고 있습니다. 나는 네가하려는 것을보고 있지만, 두 가지 형태를 인스턴스화하여 서로를 바보로 만든다. 서로 알지 못해서 confirmCall 대리인이 null이됩니다.

쉽게 수정할 수 있습니다.

OP는 전역 입력란에 인스턴스를 지정하려고하기 때문에. 우리는 이것을 이렇게 선언합니다. 또한 에서 메소드를 삭제했음을 유의하십시오. 이는 필요하지 않아야합니다.

private static Program programInstance = new Program(); 

이렇게함으로써, 당신은 당신의 Program 클래스의 인스턴스를 얻을거야, 그리고 당신의 Form1이 전달하여 양식 클래스에서 사용할 수 있습니다.

Form1 form = new Form1(programInstance); 
Application.Run(form); 

이렇게하면 인스턴스가 하나만 있습니다. 하지만 IMO는 실제로 단 하나의 인스턴스가 필요한 경우이 경우 SingleTon 패턴을 사용할 수 있습니다.

싱글 톤에 대한 참조로 나는 Singleton pattern에 대한 존 스키켓 블로그를 살펴볼 것을 제안합니다.

+0

이 오류가 발생했습니다 :'비 정적 필드, 메서드 또는 속성 'Program.programInstance''에 대한 객체 참조가 필요합니다. – bernzkie

+0

@bernzkie, 정확히'Program.programInstance'로 무엇을하려하고 있습니까? 'Program' 클래스 안에'Main' Method 안에 코드 스 니펫을 삽입해야합니다. –

+0

나중에 사용할 수 있도록'programInstance'를 Global로 선언하려고합니다. : -/ – bernzkie

3
namespace DELEGATESAMPLEPROJECT 
{ 
    public class Program 
    { 
    public delegate void OnConfirmCall(); 
    public OnConfirmCall confirmCall; 

    [STAThread] 
    static void Main(string[] args) 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 

     var programRef = new Program().getReference(); // <- only one reference. 

     Application.Run(new Form1(programRef)); //Start the form1 

     programRef.startFunctionCall();//Call this function to change the Label in Form1 
    } 

    public Program getReference(){ 
     return this; 
    } 

    public void startFunctionCall(){ 
     Console.WriteLine("Function Call Started!"); //Write this to confirm the function is called 
     if(confirmCall != null){ 
      Console.WriteLine("Function Call Executing...");//Write this to confirm that the delegate is working 
      confirmCall(); 
     } 
    } 
} 
+0

'getReference' 메소드가 전혀 필요하지 않습니다. 그렇습니까? – slawekwin

+0

실제로 필요하지 않습니다. 하지만 혼란을 피하기 위해 가능한 한 자신의 코드와 일치 시키길 원했습니다. – Blaatz0r

관련 문제