具有严格On选项的Vb.NET体系结构
我将开始使用option strict on编写代码。我有一个类单元格,可以是数字、数据或字符串,包含在类表中。如何屏蔽类内部的类型处理?具有严格On选项的Vb.NET体系结构,vb.net,strict,Vb.net,Strict,我将开始使用option strict on编写代码。我有一个类单元格,可以是数字、数据或字符串,包含在类表中。如何屏蔽类内部的类型处理? 起初我认为是在接口上,但定义接口的类必须具有完全相同的签名 对于重写也是一样,我不能有不同的方法来区分返回类型 选项严格是如何工作的?我必须归还一件物品吗 编辑: 现在的课程是: Public Class cell Private pvalue As Object Public id As Long Public Formula As
起初我认为是在接口上,但定义接口的类必须具有完全相同的签名 对于重写也是一样,我不能有不同的方法来区分返回类型 选项严格是如何工作的?我必须归还一件物品吗 编辑: 现在的课程是:
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
但我认为在严格的环境下就不应该工作了
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
这个问题在这里得到了部分解释:只需使用
对象
,但可以在类中添加一个属性,告诉您在该单元格中实际使用的类型:
Public Interface ICell
Property Value As Object
Property DataType As Type
End Interface
尝试使用泛型
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)
现在还不清楚你想达到什么目的,展示一下类
单元格,至少是一个明确你意图的版本。听起来好像您可以使用两个字段,一个用于可能的数字数据,另一个用于字符串,或者可以使类generic.overrides因签名而不同-方法名+parm计数+参数类型,而不是返回类型。