杰夫,在 c# 中,最基本的:
public class Report
{
// typical simple property in report
public string ReportUid { get; set; }
// object properties
public Header Header { get; set; }
public Body Body { get; set; }
public Footer Footer { get; set; }
public Report()
{
Header = new Header();
Body = new Body();
Footer = new Footer();
}
internal void CalculateFooterTotals()
{
// summerize the lineitems values in the footer
Footer.TotalItems = Body.LineItems
.Sum(x => x.Quantity);
Footer.TotalPrice = Body.LineItems
.Sum(x => x.Total);
}
}
public class Header
{
public string Name { get; set; }
public DateTime Date { get; set; }
}
public class Body
{
public IList<LineItem> LineItems { get; set; }
public Body()
{
LineItems = new List<LineItem>();
}
}
public class LineItem
{
public string PartNumber { get; set; }
public string PartDescription { get; set; }
public int Quantity { get; set; }
public float ItemPrice { get; set; }
// computed
public float Total
{
get { return Quantity * ItemPrice; }
}
}
public class Footer
{
// populated via report.CalculateFooterTotals()
public int TotalItems { get; internal set; }
public float TotalPrice { get; internal set; }
}
一些属性当然是计算出来的,而不是 get/set。
[编辑] - 认为添加一些用法是一个好习惯,因为我看到你问道格拉斯这个问题(很可能来自数据库或其他来源):
// usage - set up report
var report = new Report {
ReportUid = Guid.NewGuid().ToString(),
Header =
{
Name = "My new report",
Date = DateTime.UtcNow
}};
// add lineitems to body (in real case, probably a loop)
report.Body.LineItems.Add(new LineItem()
{
Quantity = 1,
ItemPrice = 12.30f,
PartDescription = "New shoes",
PartNumber = "SHOE123"
});
report.Body.LineItems.Add(new LineItem()
{
Quantity = 3,
ItemPrice = 2.00f,
PartDescription = "Old shoes",
PartNumber = "SHOE999"
});
report.Body.LineItems.Add(new LineItem()
{
Quantity = 7,
ItemPrice = 0.25f,
PartDescription = "Classic Sox",
PartNumber = "SOX567"
});
// summerize the lineitems values in the footer
report.CalculateFooterTotals();
现在将报告应用到您的画布表面(html 等)
private static void DispalyData(Report report)
{
// set out the basics
Console.WriteLine("Header");
Console.WriteLine(report.ReportUid);
Console.WriteLine(report.Header.Date);
Console.WriteLine(report.Header.Name);
// now loop round the body items
Console.WriteLine("Items");
foreach (var lineItem in report.Body.LineItems)
{
Console.WriteLine("New Item---");
Console.WriteLine(lineItem.PartDescription);
Console.WriteLine(lineItem.Quantity);
Console.WriteLine(lineItem.ItemPrice);
Console.WriteLine(lineItem.PartNumber);
Console.WriteLine(lineItem.Total);
Console.WriteLine("End Item---");
}
// display footer items
Console.WriteLine("Footer");
Console.WriteLine(report.Footer.TotalItems);
Console.WriteLine(report.Footer.TotalPrice);
}
// called in code as:
DispalyData(report);
希望这扫描正常...将其推送到社区 wiki(通过编辑),因为它是一个普遍追捧的主题。
[顺便说一句] - 尽管你会知道 c# 到 vb.net 转换器,我试过这个,它看起来很有希望: http: //www.developerfusion.com/tools/convert/csharp-to-vb