2013-10-09 159 views
2

我有一個看起來像一個簡單的問題,但由於某種原因,我有一個問題讓我的大腦圍繞着一個具有多個對象的對象的概念。例如,假設我們有一個包含頁眉和頁腳的對象,其中包含多個對象。在一個對象中創建一個對象類

和報告一樣,標題的名稱和地址也是如此。頁腳會包含購買商品的總數。介於兩者之間的是包含零件號,說明和價格的訂單項。

我想我可以有一個對象的頁眉,頁腳和一個行項目對象的數組,所有的都有自己的屬性。我以報告爲例,因爲這是我能想到的唯一概念,它將接近解釋我的問題。

有人可以給我一個鏈接或解釋如何創建這種類型的對象。

我使用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 
+1

使其更加具體。這實際上是關於報告(序列關鍵)還是關於課程設計的更普遍? –

+2

這聽起來像你有你的答案。具有其屬性和表示「LineItems」集合的屬性的標頭對象。這聽起來非常合理。 – Khan

+0

任何具有類屬性的解決方案(如下所示)都只能存儲內容。任何佈局知識(比如:標題都在頂部)必須以其他方式存儲。 –

回答

4

傑夫,在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; } 
} 

有些屬性是當然的計算,而不是獲取/套。

[編輯] - 認爲它會是很好的做法,增添幾分使用的,因爲我看到你問道格拉斯這個問題(不是從數據庫或其他來源可能更多):

// 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

+0

謝謝吉姆,這真的可以幫助我理解和試驗你答案的用法。儘管我真正的問題要複雜得多,但這確實有助於我讓自己的大腦圍繞班級和該班的重複部分進行。我相信除了這個以外,還有更多的東西可以用來學習一般的課程,但是這會讓我走。我還有一個問題,我看到如何定義它並加載它,如何輸出它?比方說把它顯示到控制檯上。 – Jeff

+0

jeff ...讓我添加一個快速顯示部分... –

+0

神奇的吉姆!! ... ..這完全解釋了基礎知識,並一定會幫助別人。謝謝你在這裏的時間。 – Jeff

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 
+0

謝謝道格拉斯 - 你能告訴我如何初始化和添加項目到lineitem? – Jeff

+0

你有幾種方法。您可以爲每個構造函數創建一個構造函數並初始化其中的屬性,也可以使用默認構造函數並手動設置屬性。 –

+0

另外,您不能將項目添加到'IEnumerable',因爲它沒有'.Add'方法。如果要添加到ICollection中,您需要將其更改爲「ICollection」。 –