vb.net 在对象中创建对象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19274973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a Class of Objects within an Object
提问by Lynn
I have what might seem like a simple question, but for some reason I am having a problem getting my brain around the concept of having a object with multiple object with in it. For example lets say we have a object with a header and a footer with multiple objects in between.
我有一个看似简单的问题,但出于某种原因,我在让我的大脑围绕其中包含多个对象的对象的概念时遇到了问题。例如,假设我们有一个带有页眉和页脚的对象,它们之间有多个对象。
Like a report, the header would have the name and address. The footer would have a total of the item that where bought. In between would be the line items with the part number, description and a price.
像报告一样,标题将包含名称和地址。页脚将包含购买的项目的总数。中间是带有零件号、描述和价格的行项目。
I guess the I can have a object with an header, footer and an array of line item object, all with their own properties. I am using a report as the example, because it's the only concept that I can think of that will get close to explaining my question.
我想我可以拥有一个带有页眉、页脚和一系列行项目对象的对象,所有这些对象都有自己的属性。我以报告为例,因为这是我能想到的唯一接近于解释我的问题的概念。
Can someone please send me a link to or explain on how to create this type of object(s).
有人可以给我发送一个链接或解释如何创建这种类型的对象。
I am using VS 2010 and VB.net and I can translate from C# to VB.
我正在使用 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
回答by jim tollan
Jeff, in c# and at its most basic:
杰夫,在 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; }
}
Some of the properties are of course computed, rather than get/set.
一些属性当然是计算出来的,而不是获取/设置。
[edit]- thought it'd be good practice to add a bit of usage, as I saw you ask Douglas this question (more than likely from DB or other source):
[编辑]- 认为添加一些用法是个好习惯,因为我看到你问道格拉斯这个问题(很可能来自 DB 或其他来源):
// 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();
now apply report to your canvas surface (html etc..)
现在将报告应用到您的画布表面(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);
Hope this scans ok... pushed it to community wiki (via edits), as it's a universally sought after topic.
希望这扫描没问题...将它推送到社区维基(通过编辑),因为它是一个普遍追捧的主题。
[btw] - altho you'll be aware of c# to vb.net convertors, I tried this one and it looks pretty promising: http://www.developerfusion.com/tools/convert/csharp-to-vb
[btw] - 虽然你会知道 c# 到 vb.net 转换器,我试过这个,它看起来很有希望:http: //www.developerfusion.com/tools/convert/csharp-to-vb
回答by Douglas Barbin
You need to create a class for each type of object. It's best to give each of them their own file.
您需要为每种类型的对象创建一个类。最好给他们每个人自己的文件。
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
Use it like so:
像这样使用它:
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

