选项严格的Vb.NET架构

时间:2015-06-16 08:15:16

标签: vb.net strict

我会开始使用option strict编码。我有一个类Cell,可以是数字,数据或字符串,包含在类表中。如何掩盖类中的类型处理?
起初我想在一个接口,但定义接口的类必须具有完全相同的签名。

对于覆盖相同,我不能有不同的方法来区分自己只是为了返回类型...

Option Strict On如何运作?我必须返回一个对象吗?

编辑: 现在课程是:

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

但我认为在Strict On的背景下不应该有用。

 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

问题在此部分解释:improve the exception handling vb.net

2 个答案:

答案 0 :(得分:2)

尝试使用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

然后这两个都是正确的:

    Dim newCell As New cell(Of Double)
    Dim strCell As New cell(Of String)

答案 1 :(得分:0)

坚持使用Object,但可能会在您的类中添加一个属性,告诉您在该单元格中实际使用的类型:

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