我看起来似乎是一个简单的问题,但由于某种原因,我遇到了一个问题,让我的大脑围绕着一个带有多个对象的对象的概念。例如,假设我们有一个带有标题和页脚的对象,其间有多个对象。
与报告一样,标题会有名称和地址。页脚将总共有购买的项目。介于两者之间的是具有部件号,描述和价格的订单项。
我想我可以拥有一个带有页眉,页脚和行项目对象数组的对象,它们都有自己的属性。我使用报告作为例子,因为这是我能想到的唯一一个能够接近解释我的问题的概念。
有人可以给我发一个链接或解释如何创建这种类型的对象。
我正在使用VS 2010和VB.net,我可以从C#转换为VB。
Report Object
Header Object
Property Name
Property Date
End
LineItem() Array Object
Property Part Number
Property Part Description
Property Number of Items
Property Per Item Price
Property Total price
End
Footer Object
Property Total Items count
Property Total Price
End
End
答案 0 :(得分:5)
杰夫,在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; }
}
某些属性当然是计算出来的,而不是获取/设置。
[edit] - 认为添加一些用法是一种好习惯,因为我看到你问道格拉斯这个问题(更多可能来自数据库或其他来源):
// 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);
希望这可以扫描好...将其推送到社区维基(通过编辑),因为它是一个普遍受欢迎的主题。
[btw] - 虽然你会知道c#到vb.net转换器,我试过这个并且看起来非常有希望:http://www.developerfusion.com/tools/convert/csharp-to-vb
答案 1 :(得分:3)
您需要为每种类型的对象创建一个类。最好给每个人自己的文件。
Public Class Report
Public Property Header As Header
Public Property LineItems As IEnumerable(Of LineItem)
Public Property Footer As Footer
End Class
Public Class Header
Public Property Name As String
' This probably isn't the best word choice because it is a type alias in VB
Public Property [Date] As Date
End Class
Public Class LineItem
Public Property PartNumber As Integer
Public Property PartDescription As String
Public Property NumberOfItems As Integer
Public Property PerItemPrice As Decimal
Public Property TotalPrice As Decimal
End Class
Public Class Footer
Public Property TotalItemsCount As Integer
Public Property TotalPrice As Decimal
End Class
像这样使用它:
Dim myReport As New Report()
Dim myHeader As New Header()
Dim lineItem1 As New LineItem()
Dim lineItem2 As New LineItem()
Dim myFooter As New Footer()
With myReport
.Header = myHeader
.Footer = myFooter
.LineItems = {lineItem1, lineItem2}
End With