VB2005の機能を,文面の知識から,体得し知恵に昇華しようとしています。
Genericの記法が class xxxx( of T)。
Null可能な変数が nullable(of T)。
いままで点知識でしかなかったのですが....
Public Class extValueType(Of T)
::::
End Public
と定義した際に、二つを結ぶと,
Dim a As New extValueType(Of Nullable( of Int32))
という使用ができるのですね。
早速, テストしてみました。
Public Class extValueTypeBase
Public Val_ As Object
'**
Public Overridable Function setValue(ByVal val As Object) As Boolean
Return Nothing
End Function
'**
Public Property Val() As Object
Get
Return Val_
End Get
Set(ByVal value As Object)
Val_ = value
End Set
End Property
'**
End Class
'*
Public Class extValueType(Of T)
Inherits extValueTypeBase
'**
Public Overrides Function setValue(ByVal val As Object) As Boolean
value = DirectCast(val, T)
End Function
'**
Public Property value() As T
Get
Return val
End Get
Set(ByVal value As T)
val_ = value
End Set
End Property
End Class
[test.vb]
Dim a As New extValueType(Of Nullable(Of Int32))
Dim b As New extValueType(Of Nullable(Of DateTime))
Dim c As extValueTypeBase = a
a.setValue(3)
b.setValue(New Date(2000, 2, 3))
という記述が可能になる.........
objectで受けるので Boxingが避けられないのが引っかかるが, ソースが短くなるのでいいかな?
ジェネリック万歳 : (こんな使い方は邪道なのかな?)