0

我知道标题具有误导性,但我想不出更好的方法来解决这个问题。我有一个显示表单的程序,允许用户从其他程序中选择运行。然后我试图创建一些通用代码来调用表单。在不复制代码的情况下不确定最佳方法。这是我想做的;

        TaxYearForm loTaxYear = new TaxYearForm();

        var loTaxProgram = new BaseForm();

        if (loTaxYear.ShowDialog() == DialogResult.OK)
        {
            string lsTaxModule = loTaxYear.gsTaxModule.ToUpper();
            int lnTaxYear = loTaxYear.gnPrimaryFilter;

            switch (lsTaxModule)
            {
                case "RE":
                    RealEstateForm loTaxProgram = new RealEstateForm();

                    break;
                case "PC":
                    PerCapitaForm loTaxProgram = new PerCapitaForm();

                    break;

            }

            loTaxProgram.gsUserId = gsUserId;
            loTaxProgram.gnPrimaryFilter = lnTaxYear;
            loTaxProgram.gnSecurityLevel = gnSecurityLevel;

            loTaxProgram.ShowDialog();

我不能这样做,因为在 switch 语句中我重新使用了变量 loTaxProgram。我可以将所有代码放在每个 case 语句中,但这是我想要避免的。我在类中创建了一个方法,但由于必须将参数声明为 BaseForm,因此遇到了同样的问题。

4

4 回答 4

2

我建议创建一个新的工厂类来创建表单:

public class FormFactory
{
    public BaseFrom Create(string taxCode)
    {
        switch (taxCode)
        {
            case "RE":
                return new RealEstateForm();

                // rest of stuff.
        }
    }
}

所以你的代码可能看起来像这样:

if (loTaxYear.ShowDialog() == DialogResult.OK)
{    
    var factory = new FormFactory();

    var loTaxProgram = factory.Create(loTaxYear.gsTaxModule.ToUpper());

    loTaxProgram.gsUserId = gsUserId;
    loTaxProgram.gnPrimaryFilter = loTaxYear.gnPrimaryFilter;
    loTaxProgram.gnSecurityLevel = gnSecurityLevel;

    loTaxProgram.ShowDialog();
}

因此,工厂类将负责创建一个表单,让事情变得更简洁。

于 2012-08-10T13:28:31.180 回答
1

无需进一步重新申报

switch (lsTaxModule)
{
  case "RE":
    loTaxProgram = new RealEstateForm();

    break;
  case "PC":
    loTaxProgram = new PerCapitaForm();

    break;
}
于 2012-08-10T13:22:32.050 回答
1

如果您的两个表单都来自您的 BaseForm 则不要重新声明您的loTaxProgram变量。

        TaxYearForm loTaxYear = new TaxYearForm();

        BaseFrom loTaxProgram;

        if (loTaxYear.ShowDialog() == DialogResult.OK)
        {
            string lsTaxModule = loTaxYear.gsTaxModule.ToUpper();
            int lnTaxYear = loTaxYear.gnPrimaryFilter;

            switch (lsTaxModule)
            {
                case "RE":
                    loTaxProgram = new RealEstateForm();

                    break;
                case "PC":
                    loTaxProgram = new PerCapitaForm();

                    break;

            }

            loTaxProgram.gsUserId = gsUserId;
            loTaxProgram.gnPrimaryFilter = lnTaxYear;
            loTaxProgram.gnSecurityLevel = gnSecurityLevel;

            loTaxProgram.ShowDialog();
于 2012-08-10T13:22:45.093 回答
1

最快的方法是这样做(注意 case 语句的变化):

    TaxYearForm loTaxYear = new TaxYearForm(); 

    var loTaxProgram = new BaseForm(); 

    if (loTaxYear.ShowDialog() == DialogResult.OK) 
    { 
        string lsTaxModule = loTaxYear.gsTaxModule.ToUpper(); 
        int lnTaxYear = loTaxYear.gnPrimaryFilter; 

        switch (lsTaxModule) 
        { 
            case "RE": 
                loTaxProgram = new RealEstateForm(); 

                break; 
            case "PC": 
                loTaxProgram = new PerCapitaForm();

                break; 

        } 

        loTaxProgram.gsUserId = gsUserId; 
        loTaxProgram.gnPrimaryFilter = lnTaxYear; 
        loTaxProgram.gnSecurityLevel = gnSecurityLevel; 

        loTaxProgram.ShowDialog(); 

为了将来参考,您可能想在 IMO 上搜索非常适合这种代码的工厂模式。

于 2012-08-10T13:23:26.420 回答