2016-06-13 28 views
-1

只有第二个问题在这里提出,所以如果我遗漏了一些代码可能会帮助您评论这么说。C#如何在方法运行之前使用派生类变量设置基类变量

我有一个抽象类Report,派生类叫做BusinessBreakdown,派生类叫做BusinessWrittenBreakdown。

如何更改此代码,使BusinessWrittenBreakdown中的ReportTitle,DateField和DataLabel在BuildDefinition方法运行之前在BusinessBreakdown中设置具有相同名称的构造函数。

我试过使用谷歌搜索和所有,但不能解决如何,这是我得到。出现的错误似乎没有帮助,所以我更多地寻求一种可行的不同方法。

public class BusinessBreakdown : Report 
{ 
    static string ReportTitle; 
    static string DateField; 
    static string DateLabel; 

    public BusinessBreakdown(string theReportTitle, string theDateField, string theDateLabel) 
     : base(BuildDefinition) 
    { 
     ReportTitle = theReportTitle; 
     DateField = theDateField; 
     DateLabel = theDateLabel; 
    } 

    /// <summary> 
    /// Build the report definition 
    /// </summary> 
    /// <returns></returns> 
    public static ReportDef BuildDefinition(Settings settings) 
    { 

     // Create definition 
     var rdef = new ReportDef(); 

     // Create configuration context 
     var context = new FsConfigContext(); 
     rdef.ConfigContext = context; 

     // Report title 
     rdef.ReportTitle = ReportTitle; 

     // Create report date range configuration 
     ConfigDateRange drange = new ConfigDateRange(settings, "ReportDate", Config.ConfigDisposition.Filter, 
             new FilterExpressionDef 
             { 
              Expression = DateField 
             }, DateLabel); 

     rdef.ReportDate = drange; 

///// code ... 

     return rdef; 
    } 
} 
} 


public class BusinessWrittenBreakdown : BusinessBreakdown 
{ 
    // Report title 
    static string ReportTitle = "Business Written Breakdown Report"; 
    // Report date range and label 
    static string DateField = "COMMISS.BRDateWritten"; 
    static string DateLabel = "Written Date"; 

    public BusinessWrittenBreakdown() 
     : base(ReportTitle, DateField, DateLabel) 
    { 
    }   
/// more code... 
    } 
} 
+5

你的领域为什么是静态的? – sstan

+5

你说你得到一个错误,但不要说什么,或者它在哪里。 –

+3

它不清楚你在问什么。你能举一个你想要发生什么的例子吗? –

回答

0

你完全无法控制该序列。这是一个static方法。

public static ReportDef BuildDefinition(Settings settings) 

根据定义,有人可能会叫不不断这个方法调用任何类的构造函数。因此,它无法让他们首先调用构造函数。

静态属性和方法在类的所有实例之间共享。你有一个具有这些不同属性的构造函数表明你想为一个报表创建一个类的实例,然后为另一个报表创建另一个具有不同参数的实例。

在这种情况下,它看起来并不像你需要静态方法,而只是增加了复杂性。所以我会删除static。这意味着每个属性或字段都是“实例”属性或字段。它属于类的每个单独实例,而不是在类的实例之间共享。

+0

谢谢,这是有道理的,自大学以来,我一年没有编写很多程序,并忘记了一些简单的东西。 其余的代码是否可以在没有静态方法的情况下工作,还是需要改变我获取变量的方式? –

相关问题