I am trying to design a table type structure that contains rows and cells. I intend the cell class to have a value property. I’m trying to work out how I can create a value that can return some defined different types e.g integer, single, date, string. I’d like the cell value to be strongly typed, but I am unsure how best to get this working.
My thinking in code so far:
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
Here a simple solution using basic inheritance and even without generics. The idea is that you have a single parent class for all cells. And a different cell class per column type. This make possible to declare list of cells in a Row object. Because of there may be different cell types within a single row, you can’t declare it (list of cells) as a list of some specific cell type. But you can use cell’s parent class in this case. In short here is example: