【发布时间】:2012-11-20 16:03:26
【问题描述】:
使用 C#.NET4.5 和 Visual Studio 2012 Ultimate。
我目前正在尝试使用我的标签打印程序 Ive Used Interfaces 之前的抽象类。
我使用接口来解耦我的两个类,效果很好。
现在我正在尝试以下内容。
第一个。我的抽象类...
abstract class Label
{
public virtual IList<Microsoft.Reporting.WinForms.ReportParameter> NewReportSetup(string part, string batch, string locn, string wheel, string gear, string length,
string fits, string newbar, string newbarnum, string abs)
{
IList<Microsoft.Reporting.WinForms.ReportParameter> parameters = new List<Microsoft.Reporting.WinForms.ReportParameter>();
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramPart", part));
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramBatch", batch));
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramLocn", locn));
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramWheel", wheel));
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramGear", gear));
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramLength", length));
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramABS", abs));
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramBuyer", fits));
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramBarCode", newbar));
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramBartxt", newbarnum));
return parameters;
}
}
第二次。我的 ReportShaft 继承标签...
class ReportShaft : Label
{
public virtual IList<Microsoft.Reporting.WinForms.ReportParameter> NewReportSetup()
{
return new List<Microsoft.Reporting.WinForms.ReportParameter>();
}
}
3rd. 我的表单实例化 ReportShaft 类并调用 NewReportSetup()...
private void NewReportSetupSHAFT()
{
if(txtABS.Text.ToString() == "" || txtABS.Text == null)
{
txtABS.Text = "N/A";
}
IList<Microsoft.Reporting.WinForms.ReportParameter> param = new List<Microsoft.Reporting.WinForms.ReportParameter>();
param = reportshaft.NewReportSetup(txtNewPart.Text.ToString(),
txtBatch.Text.ToString(), txtLocation.Text.ToString(), txtWheel.Text.ToString(), txtGear.Text.ToString(), txtLength.Text.ToString(),
txtFits.Text.ToString(), txtNewBar.Text.ToString(), txtNewBarNum.Text.ToString(), txtABS.Text.ToString());
reportViewer1.LocalReport.SetParameters(param);
}
这很好用(虽然我感觉我以错误的方式使用抽象类,不确定)。
我的问题是:
我想创建一个新的报表类。我希望类调用非常相同的方法,但让我更改前2个参数名称,并跳过最后一个完全。
现在这需要对方法进行 overide 吗?如果是这样,人们将如何做到这一点? Label 方法是否需要从 Virtual Function 进行更改?
非常感谢各位!
更新:: 好吧,当我提到参数时,似乎有些混乱。
我的意思是说我希望从我的抽象类中调用 1 方法,然后在我继承此标签类和方法的报告类中,我希望更改“报告参数”,我的意思是方法的主体。
这样做的原因是,如果我只是简单地创建另一个方法并为每个不同的报告调用它,我将使用几乎相同的方法。
这里的例子:: 改变这个..
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramPart", part));
这也是..
parameters.Add( new Microsoft.Reporting.WinForms.ReportParameter("paramchanged!!", part));
这是一个例子。因此,根据我收集到的信息,我在我的报告类中覆盖了我的标签类方法。但是后来我卡住了,如果我尝试更改正文,我必须输入其余的代码。对我来说,我仍然会得到一堆看起来相同的方法。
有没有办法改变“方法主体的部分部分”而不必输入其余部分。
希望这能消除混乱。
【问题讨论】:
标签: c# overriding virtual abstract-class