2016-08-01 2 views
0

현재 (저는 MBG SimpleWizard 라이브러리를 사용하여) 마법사를 작성하고 있습니다. 나는 여러 페이지를 가지고있다. 그들간에 데이터를 공유하는 방법으로 클래스 out DBManip DBController이 전달됩니다. 메서드에서이 DBController를 사용해야하지만 라이브러리에서 라이브러리를 처리하므로 메서드에 대한 참조로 DBController를 쉽게 전달할 수 없습니다. 전달 된 참조를 메소드가 수정할 수있는 속성으로 만들 수 있고 참조를 보존 할 수 있습니까?전달 된 참조를 메소드에 사용 가능하게 만들기

클래스의 초기화 :

WizardHost host = new WizardHost(); 
    using (host) 
    { 
     host.Text = Migration.Properties.Resources.AppName; 
     host.ShowFirstButton = false; 
     host.ShowLastButton = false; 
     host.WizardCompleted += new WizardHost.WizardCompletedEventHandler(this.Host_WizardCompleted); 

     DBManip DBController; 

     host.WizardPages.Add(1, new Page1()); 
     host.WizardPages.Add(2, new Page2(out DBController)); 
     host.WizardPages.Add(3, new Page3(out DBController)); 
     host.WizardPages.Add(4, new Page4(out DBController)); 
     host.LoadWizard(); 
     host.ShowDialog(); 
    } 

생성자 :

public Page2(out DBManip DBController) 
     { 
      this.InitializeComponent(); 
      this.label1.Text = Migration.Properties.Resources.ExportDirectoryMessage; 
      this.exportDirTextbox.Text = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
     } 

방법 :

private bool SetExportDirectory() 
{ 
    string exportDirectory = this.exportDirTextbox.Text; 

    // If a path is given, check if it's valid 
    // and set the pathExists boolean 
    if (!Directory.Exists(exportDirectory)) 
    { 
     MessageBox.Show(Migration.Properties.Resources.InvalidPath); 
     return false; 
    } 

    // Initializing the object to manipulate the databases 
    exportDirectory = new DBManip(exportDirectory); 
    return true; 
} 

재산권 메서드를 호출합니다

public bool PageValid 
{ 
    get { return SetExportDirectory(); } 
} 

죄송합니다. C#을 처음 사용하는 분들에게 매우 익숙합니다.

+0

당신이 당신의 페이지 클래스 (페이지 1, 페이지 2 등을 모두 원하는 건가요)를 사용하여 'DBManip'의 공통 인스턴스에 대한 참조를 공유하고,'SetExportDirectory'가'DBManip'의 해당 인스턴스로 무엇인가를하기를 원합니까? 어떤 클래스가'SetExportDirectory' 멤버인가요? –

+0

첫 번째 질문은 그렇습니다. 두 번째로는 PageValid와 마찬가지로 Page2의 멤버입니다. (Page1에는 필요하지 않기 때문에 참조가 없지만 아이디어를 얻을 수 있습니다.) –

+0

'DBManip'을 사용하고자하는 것만 추측 할 수 있지만, Page 클래스 중 하나의 DBManip 인스턴스에 대한 답을 제공했습니다. –

답변

0

귀하의 페이지가 DBManip으로 무엇을하는지는 분명하지 않지만이를 사용하는 페이지 클래스의 속성으로 지정해야합니다 그것.

이렇게하려면 일반적으로 페이지를 만들기 전에 DBManip 인스턴스를 만들고이를 원하는 각 생성자에 전달하십시오. 각 클래스에는 나중에 참조 할 수 있도록 참조를 저장하는 속성이 있습니다. 클래스는 자신의 공통 기본 클래스를 쉽게 제공 할 수 없기 때문에 클래스 자체에 대해 해당 속성을 선언해야합니다.

하지만 나중에 만들게됩니다. 생성자가 종료 된 후에 생성 된 객체에 대한 참조를 공유하기 위해 다른 클래스가 필요하기 때문에 빠른 "참조"제네릭 클래스를 추가하고 모두 참조를 공유합니다. 그런 다음 속성을 변경할 수 있으며이 작은 "핸들"클래스의 기존 인스턴스에 새 속성 값이 있습니다.

Reference.cs

// Semantically, this is basically a pointer to a pointer, without the asterisks. 
public class Reference<T> 
{ 
    public Reference() { } 

    public Reference(T t) { Value = t; } 

    public T Value; 
} 

홈페이지 C#

WizardHost host = new WizardHost(); 
using (host) 
{ 
    host.Text = Migration.Properties.Resources.AppName; 
    host.ShowFirstButton = false; 
    host.ShowLastButton = false; 
    host.WizardCompleted += new WizardHost.WizardCompletedEventHandler(this.Host_WizardCompleted); 

    // ************************ 
    // Create shared "reference" instance 
    // ************************ 
    Reference<DBManip> dbControllerRef = new Reference<DBManip>(); 

    host.WizardPages.Add(1, new Page1()); 
    host.WizardPages.Add(2, new Page2(dbControllerRef)); 
    host.WizardPages.Add(3, new Page3(dbControllerRef)); 
    host.WizardPages.Add(4, new Page4(dbControllerRef)); 
    host.LoadWizard(); 
    host.ShowDialog(); 
} 

Page2.cs

// It's not an out parameter so don't make it one. 
public Page2(Reference<DBManip> dbControllerRef) 
{ 
    this.InitializeComponent(); 

    this.DBControllerRef = dbControllerRef; 

    this.label1.Text = 
     Migration.Properties.Resources.ExportDirectoryMessage; 
    this.exportDirTextbox.Text = 
     Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
} 

public Reference<DBManip> DBControllerRef { 
    get; private set; 
} 

private bool SetExportDirectory() 
{ 
    string exportDirectory = this.exportDirTextbox.Text; 

    // If a path is given, check if it's valid 
    // and set the pathExists boolean 
    if (!Directory.Exists(exportDirectory)) 
    { 
     MessageBox.Show(Migration.Properties.Resources.InvalidPath); 
     return false; 
    } 

    // Everybody has the same Refernece<DBManip> 
    this.DBControllerRef.Value = new DBManip(exportDirectory); 

    return true; 
} 
+0

그래서 2 페이지에서 DBController의 매개 변수 중 하나를 설정하면 3 페이지에서 해당 매개 변수에 액세스 할 수 있습니까? C++에서 오는 C# 암시적인 참조 정보는 혼란 스러울 수 있습니다. –

+0

컨트롤러를 사용하는 것에 관해서는 파일 시스템에서 작동합니다. 2 페이지에 제공된 디렉토리를 저장해야하며, 3 페이지에서 제공된 디렉토리를 기반으로 작업을 수행합니다. –

+0

@AdrianSmith Right.이를 자동으로 참조 해제 된 포인터라고 생각하거나 C++ 참조와 달리 나중에 다른 실제 인스턴스로 다시 바인드 할 수 있다는 점을 제외하면 'DBManip & DBController'매개 변수로 생각할 수 있습니다. –

관련 문제