独自コレクションクラスを作成する VB.NET2005 
 		
	
	 
	VB6だとウィザードで知らない間に作ることができたコレクションクラス(配列クラス)だが、VB2005だと自分で作る必要がある。 
	といっても、その手の質問が出るだろうということでMSもチュートリアルをここで公開している。 
	それを参考に簡単なものを作ってみた(チュートリアルで充分という方はご愛嬌)。 
	ついでに継承クラスの復習になるはず。 
	 
	’これが今回配列の項目として作りたいもの 合計4項目の文字列を配列化したい。これができると独自の配列クラスが作れるので便利
	 
	Public Class MyItem 
	    Public ID As String 
	    Public Name As String 
	    Public BDay As String 
	    Public Adre As String 
	End Class
  
	 |   
	 
	 
	 
	’こちらが前の4項目の文字列を配列にするクラス
	 
	Public Class Collec 
	    Inherits System.Collections.CollectionBase 'コレクションクラスを継承している 
	 
	’MSDNの「CollectionBaseの明示的インターフェイスの実装」で紹介されているAddを実装する。
	 
	    Public Sub add(ByVal Citem As MyItem) 
	        List.Add(Citem) ’追加する 
	    End Sub 
	 
	MSのコピペ これも「CollectionBaseの明示的インターフェイスの実装」=要するに自分(手前)で書いといて!ということ。
	 
	    Public Sub remove(ByVal index As Integer) 
	        If index > Count - 1 Or index < 0 Then 
	            ' If no widget exists, a messagebox is shown and the operation is  
	
	            ' cancelled. 
	
	            System.Windows.Forms.MessageBox.Show("Index not valid!") 
	        Else 
	            ' Invokes the RemoveAt method of the List object. 
	
	            List.RemoveAt(index) 
	        End If 
	 
	    End Sub 
	 
	これも「CollectionBaseの明示的インターフェイスの実装」で紹介されている「item」の実装。 
	MSの例では読み取り専用として作られていたが、ここでは書き込みも可とする
	 
	    Public Property Item(ByVal index As Integer) As MyItem 
	        Get 
	            Return CType(List.Item(index), MyItem) 
	           'このListは「CollectionBaseのプロテクトプロパティー。他にInnerList というArrayListもある。」 
	           '要するに継承「親の七光り=継承もとのクラスの機能を引き継いで実装できる」というわけだ 
	           '便利の一言に尽きる。 
	        End Get 
	        Set(ByVal value As MyItem) 
	            List.Item(index) = value 
	        End Set 
	    End Property 
	 
	 
	End Class
  
	 |   
	 
	 
	「MyItem」クラスを配列化したのが「Collec」クラス。反対にコレクションの項目は「MyItem」を書き換えれば変更できるということになる。 
	 
	使い方は、 
	 
	 
	Public Class Form1 
	    Dim Myurl As New Collec 
      Dim it As New MyItem 
      it.ID = "1" 
      it.Name = "balard" 
      it.BDay="20071020" 
      it.Adre="NewYork" 
      Myurl.add(it) 
      it = Nothing 
      '他にも	'Myurl.Count’Countは書いていないのに「継承」したので使える。「CollectionBaseのパブリック プロパティ」 
      'Myurl.Item(i).Name = "  'これはじぶんでつくったやつ"
  
      etc etc.................... 
	  
  
  	 |  
  
	
	
	 
	
	
		
		
	 
	
	 |