2009/06/13(Sat) 04:15:38 編集(投稿者)
すみません。しつこいですがクラスバージョンもサンプル作ってみたので載っけておきます。
もっと良い方法があればご教示いただけますとありがたいですm(_ _)m
-------------------------------------------------------------------------------------------------------
Public Class Array2
Public Shared Sub test()
Dim aryTest As New ArrayList
Dim Man1 As New MemberNN(1, "N1", Today())
Dim Man2 As New MemberNN(2, "N2", Today())
Dim Man3 As New MemberNN(3, "N3", Today())
Dim Man4 As New MemberNN(2, "N2", Today())
Try
aryTest.Add(Man1) 'Item(0)に{1,"N1",Today()}をAdd
aryTest.Add(Man2) 'Item(1)に{2,"N2",Today()}をAdd
'aryTestの中身をテスト出力
Console.WriteLine(String.Format("aryTest.Count:{0}", aryTest.Count))
Console.WriteLine(String.Format("aryTest.Item(0):{0}{1}{2}{1}{3}", aryTest.Item(0).N1, ",", aryTest.Item(0).N2, aryTest.Item(0).N3))
Console.WriteLine(String.Format("aryTest.Item(1):{0}{1}{2}{1}{3}", aryTest.Item(1).N1, ",", aryTest.Item(1).N2, aryTest.Item(1).N3))
'Containsメソッド確認用テスト出力
If aryTest.Contains(Man3) Then
Console.WriteLine("MAN3はAddされておらずここは通らない")
ElseIf aryTest.Contains(Man1) Then
Console.WriteLine("MAN1は既にAdd済みのためここは通る")
End If
If aryTest.Contains(Man4) Then
Console.WriteLine("N1〜N3の値はMan2=Man4だが、AddしていないMan4はContainsで見つからずここは通らない")
'⇒つまり、Containsメソッドは存在はするが意図した使用が不可能(T_T)
End If
'Clearメソッド確認
aryTest.Clear()
Console.WriteLine(String.Format("aryTest.Count:{0}", aryTest.Count))
Catch ex As Exception
Throw
Finally
'Newしただけに、DisposeかNothing入れとくべき!?要素分処理記述はきついですね(T_T)
Man1 = Nothing : Man2 = Nothing : Man3 = Nothing : Man4 = Nothing
End Try
End Sub
-------------------------------------------------------------------------------------------------------
Public Class MemberNN
Private _n1 As Integer
Private _n2 As String
Private _n3 As Date
Sub New(ByVal _nn1 As Integer, ByVal _nn2 As String, ByVal _nn3 As Date)
_n1 = _nn1 : _n2 = _nn2 : _n3 = _nn3
End Sub
Public ReadOnly Property N1() As Integer
Get
Return _n1
End Get
End Property
Public ReadOnly Property N2() As String
Get
Return _n2
End Get
End Property
Public ReadOnly Property N3() As Date
Get
Return _n3
End Get
End Property
End Class
End Class
-------------------------------------------------------------------------------------------------------
==出力==
aryTest.Count:2
aryTest.Item(0):1,N1,2009/06/13 0:00:00
aryTest.Item(1):2,N2,2009/06/13 0:00:00
MAN1は既にAdd済みのためここは通る
aryTest.Count:0
-------------------------------------------------------------------------------------------------------