Ognacの雑感

木漏れ日々

目次

Blog 利用状況

書庫

ギャラリ

Generic 万歳 ( for VB2005)

 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が避けられないのが引っかかるが, ソースが短くなるのでいいかな?
  ジェネリック万歳 : (こんな使い方は邪道なのかな?)

 


 

投稿日時 : 2006年8月22日 22:15

Feedback

No comments posted yet.
タイトル
名前
Url
コメント