Vb.NET Architecture with Option Strict On

91 views Asked by At

I would begin to code with option strict on. I have a class Cell, that can be, numeric,data or string, included in a class table. How do can mask the type handling inside the classes?
At first I thought at an Interface, but the classes that define the interface must have exactly the same signature.

The same for an override , I can not have different methods that differentiate themselves just for return type...

How does Option Strict On works? I must return an Object?

Edit: Now the class is :

Public Class cell
    Private pvalue As Object
    Public id As Long
    Public Formula As String
    Public ix As Integer
    Public lev As Integer
    Public Property Value() As Object 
        Get
            Return pvalue
        End Get
        Set(ByVal value As Object)
            pvalue = value
        End Set
    End Property
End Class

But I think that in an context of Strict On should not work.

 Public Interface ICell
    Property Value() ' I must define an univoque datatype
End Interface

Public dClass cell
    Implements ICell
    Private pvalue As Object
    Public id As Long
    Public Formula As String
    Public ix As Integer
    Public lev As Integer
    Public Property Value() As Date  Implements ICell.Value
        Get
            Return pvalue
        End Get
        Set(ByVal value As date)
            pvalue = value
        End Set
    End Property
   End Class

Public Class cell
    Implements ICell
    Private pvalue As double
    Public id As Long
    Public Formula As String
    Public ix As Integer
    Public lev As Integer
    Public Property Value() As double Implements ICell.Value
        Get
            Return pvalue
        End Get
        Set(ByVal value As Double)
            pvalue = value
        End Set
    End Property
   End Class

The problem get explained partially here :improve the exception handling vb.net

2

There are 2 answers

0
dbasnett On BEST ANSWER

Try using Generics.

Public Interface ICell(Of T)
    Property Value As T 
End Interface

Public Class cell(Of T)
    Implements ICell(Of T)
    Public Property Value As T Implements ICell(Of T).Value

    Public id As Long
    Public Formula As String
    Public ix As Integer
    Public lev As Integer
End Class

Then both of these are correct:

    Dim newCell As New cell(Of Double)
    Dim strCell As New cell(Of String)
0
sloth On

Just stick with Object, but maybe add a property to your class that tells what type you actually use in that cell:

 Public Interface ICell
    Property Value As Object
    Property DataType As Type
End Interface