2016-08-01 8 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

最初の質問には、はい。第2にはPageValidと同様にPage2のメンバです。 (Page1には参照が必要ないので、参照はありませんが、あなたはその考えを持っています)。 –

+0

私はあなたが 'DBManip'をどのように使いたいかを推測することができますが、私はページクラスの中でどのように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のパラメータの1つを設定すると、そのパラメータは3ページからアクセスできますか? C++から来るC#の暗黙の参照は混乱を招く可能性があります。 –

+0

コントローラを使用しているものに関しては、ファイルシステム上で操作を行います。 2ページで提供されたディレクトリを格納する必要があります.3ページでは、提供されたディレクトリに基づいて操作が実行されます。 @AdrianSmith Right。 –

+0

自動的に逆参照されるポインタと考えるか、C++参照とは異なり、後で別の実際のインスタンスに再バインド可能である点を除けば、パラメータ 'DBManip&DBController'と考えることができます。 –

関連する問題