2010-01-27 68 views
2

我試圖設計一個包含行和單元的表類型結構。我打算單元類有一個值屬性。我試圖找出如何創建一個可以返回一些定義的不同類型的值,例如integer,single,date,string。我希望單元格的值能被強類型化,但我不確定如何最好地實現這個功能。實現具有可以是不同類型的值屬性的單元類

我在代碼的思想至今:

Public Class Cell 
    Private _value as object 
    Public Property Value as Object // How can I define this so that it return a Type 
    Get        // e.g. integer, string, etc 
     Return _value 
    End Get 
    Set(ByVal value as object) 
     _value = value 
    End Set 
End Class 

Public Class Row 
    Dim _Cells as New List(Of Cell) 
    Public Function AddCell(ByVal c as Cell) as Cell 
    _Cells.Add(c) 
    Return _Cells(_Cells.count - 1) 
    End Function 

回答

1

這裏使用基本的繼承,甚至沒有泛型一個簡單的解決方案。這個想法是,你有一個單一的所有單元格的父類。每個列類型還有一個不同的單元類。這使得可以聲明Row對象中的單元格列表。由於單行內可能存在不同的單元格類型,因此無法將它(單元列表)聲明爲特定單元類型的列表。但在這種情況下,您可以使用單元格的父類。總之這裏是例子:

Public Class Cell 
End Class 

Public Class Cell_Id 
    Inherits Cell 
    Public Value As Integer 
End Class 

Public Class Cell_Name 
    Inherits Cell 
    Public Value As String 
End Class 

Public Class Cell_MonthlyTax 
    Inherits Cell 
    Public Value As Double 
End Class 


Public Class Row 
    Public Cells As New List(Of Cell) 

    Public Sub AddCell(ByVal cell As Cell) 
     Cells.Add(cell) 
    End Sub 
End Class 


Module Module1 

    Sub Main() 
     Dim row1 As New Row() 
     row1.AddCell(New Cell_Id() With {.Value = 1}) 
     row1.AddCell(New Cell_Name() With {.Value = "Name1"}) 
     row1.AddCell(New Cell_MonthlyTax() With {.Value = 12.2}) 
     Dim row2 As New Row() 
     row2.AddCell(New Cell_Id() With {.Value = 2}) 
     row2.AddCell(New Cell_Name() With {.Value = "Name2"}) 
     row2.AddCell(New Cell_MonthlyTax() With {.Value = 47.9}) 
     Dim row3 As New Row() 
     row3.AddCell(New Cell_Id() With {.Value = 3}) 
     row3.AddCell(New Cell_Name() With {.Value = "Name3"}) 
     row3.AddCell(New Cell_MonthlyTax() With {.Value = 73.3}) 

     Dim rows As New List(Of Row)(New Row() {row1, row2, row3}) 

     ' here you loop through rows, in each row select Cell at index 2, cast it down to a specific 
     ' type (yes, you should tell to a compiler that you sure that Cell at index 2 is of Cell_MonthlyTax type) 
     ' After casting you will get intellisence and see that Value is of Double type. 
     ' In cellsOfColumn2 you will get an array of Double (this is not exactly array, but it doesn't matter in our case) 
     Dim cellsOfColumn2 = From row In rows Select DirectCast(row.Cells(2), Cell_MonthlyTax).Value 

     ' here you may work with array of values as you want, say calculate avarange value 
     Dim result = cellsOfColumn2.Average() 
    End Sub 

End Module 
+0

感謝您的快速回復。我雖然去泛型,但你最終不得不讓每個單元格在一種類型的行中。我沒有編寫完整的模型,但類型實際上與列相關。單元格在_cells集合中的偏移量將決定列,從而確定單元格類型,因此通用方法將不起作用。 – Andrew 2010-01-27 11:55:49

+0

您能舉一個例子說明如何使用Row和Cell類來獲取Cell的值嗎?我認爲在你的場景中缺少一些東西。在任何情況下(使用泛型或對象),您都需要將值轉換爲特定類型,但不會自動失效 – Kamarey 2010-01-27 12:28:13

+0

想象一下,包含列集合和行集合的reportview類。您可以使用AddColumn方法將列添加到reportview中(列將保存元數據 - 例如columnWidth和headerText)。要添加一個新行,您可以調用reportview.newrow,並且在此方法中,您將爲報表視圖中的每個列創建一個包含新單元格的行。 – Andrew 2010-01-27 15:34:00

相關問題