在对象内创建一类对象

Lyn*_*ynn 2 c# vb.net arrays class object

我有一个看似简单的问题,但由于某种原因,我的大脑无法解决其中包含多个对象的问题。例如,假设我们有一个带有页眉和页脚的对象,中间有多个对象。

像报告一样,标题将具有名称和地址。页脚将具有购买该物品的总和。在中间的是带有零件编号,描述和价格的行项目。

我想我可以有一个带有页眉,页脚和订单项对象数组的对象,它们都具有自己的属性。我以报告为例,因为这是我能想到的唯一概念,它将接近于解释我的问题。

有人可以给我发送链接或解释如何创建这种类型的对象。

我正在使用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
Run Code Online (Sandbox Code Playgroud)

jim*_*lan 5

Jeff,在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; }
}
Run Code Online (Sandbox Code Playgroud)

当然,某些属性是计算得出的,而不是获取/设置的。

[编辑] -认为增加一些用法是一个好习惯,正如我看到的,你问道格拉斯这个问题(可能来自数据库或其他来源):

// 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();
Run Code Online (Sandbox Code Playgroud)

现在将报告应用于您的画布表面(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);
Run Code Online (Sandbox Code Playgroud)

希望一切顺利,...(通过编辑)将其推送到社区Wiki,因为这是一个广受欢迎的话题。

[btw]-虽然您会意识到vb.net转换器的C#功能,但我尝试过这一操作,它看起来很有希望:http : //www.developerfusion.com/tools/convert/csharp-to-vb