Icomparable用于比较两个对象。如果你想要将数组中的对象进行分类或者查询的时候,请使用Icomparable接口。
下面的代码显示了执行Icomparable接口的一个类:
Public Class SortClass
Implements IComparable
Private cstrValue1 As String
Public Sub New(ByVal Value1 As String)
cstrValue1 = Value1
End Sub
Public Function CompareTo(ByValobj As Object) As Integer
Implements System.IComparable.CompareTo
Dim objCompareTo As SortClass
If TypeOfobj Is SortClass
Then
objCompareTo
= obj
Select
Case objCompareTo.Value1
Case
Me.Value1
Return
0
Case
Is < Me.Value1
Return
1
Case
Is > Me.Value1
Return
-1
End
Select
Else
Throw
New System.ArgumentException( _
"Object
is not the same type.")
End If
End Function
Public Property Value1() As String
Get
Return
cstrValue1
End Get
Set(ByVal Value As String)
cstrValue1
= Value
End Set
End Property
End Class