使传递参考可用于方法

时间:2016-08-01 18:06:46

标签: c# winforms wizard

目前我正在编写一个向导(使用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#来说相当新鲜

1 个答案:

答案 0 :(得分:0)

目前还不清楚您的网页使用DBManip做了什么,但您需要将其作为使用它的任何Page类的属性。

为此,您通常在创建页面之前首先创建DBManip实例,然后将其传递给每个想要它的构造函数。每个类都有一个属性,它存储引用,以便以后可以使用它。 每个类都需要为自己声明该属性,因为您不能轻易地为它们提供自己的公共基类。

但是你以后会创建它。由于您需要不同的类来共享对构造函数退出后创建的对象的引用,因此我们将添加一个快速引用"引用"通用类,他们都会分享对它的引用。然后我们可以改变它的属性,并且它们都会在这个小小的" handle"的现有实例上都有新的属性值。类。

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;
}
相关问题