2010-01-27 51 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

相关问题