Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
Visual Basic 2010 Broncode
Namespace SortedListUsesCompareToExample
Class SomeKey :
Implements IComparable Public Value
As Integer Public Function CompareTo(
ByVal obj
As Object)
As Integer _
Implements System.IComparable.
CompareTo If obj
Is Nothing Then CompareTo = 1
ElseIf Not TypeOf obj
Is SomeKey
Then Throw New ArgumentException(
"An invalid argument was " & _
"specified. An argument of type SomeKey is required.")
Else CompareTo =
Me.Value.CompareTo(
DirectCast(obj, SomeKey).Value)
End If End Function End Class Class SomeValue Public Value
As Integer Public Overrides Function Equals(
ByVal obj
As Object)
As Boolean If obj
IsNot Nothing AndAlso TypeOf obj
Is SomeValue
Then Equals = (
Me.Value =
DirectCast(obj, SomeValue).Value)
End If End Function Public Overrides Function GetHashCode()
As Integer GetHashCode = Value
End Function Public Name
As String Public Overrides Function ToString()
As String ToString = Name
End Function End Class Class Client Public Shared Sub Main()
Dim key1
As New SomeKey With {.Value = 1}
Dim key2
As New SomeKey With {.Value = 2}
Dim key3
As New SomeKey With {.Value = 1}
Dim value1
As New SomeValue With {.Value = 1, .Name =
"value1"}
Dim value2
As New SomeValue With {.Value = 1, .Name =
"value2"}
Dim value3
As New SomeValue With {.Value = 3, .Name =
"value3"}
Dim sortedList1
As New SortedList sortedList1.Add(key1, value1)
Console.WriteLine(sortedList1.Contains(key1))
Console.WriteLine(sortedList1.Contains(key2))
Console.WriteLine(sortedList1.Contains(key3))
Console.WriteLine()
Console.WriteLine(sortedList1.ContainsKey(key1))
Console.WriteLine(sortedList1.ContainsKey(key2))
Console.WriteLine(sortedList1.ContainsKey(key3))
Console.WriteLine()
Console.WriteLine(sortedList1.ContainsValue(value1))
Console.WriteLine(sortedList1.ContainsValue(value2))
Console.WriteLine(sortedList1.ContainsValue(value3))
Console.WriteLine()
Console.WriteLine(sortedList1.Item(key1).ToString())
Console.WriteLine(sortedList1.Item(key2)
Is Nothing)
Console.WriteLine(sortedList1.Item(key3).ToString())
Console.WriteLine()
Console.WriteLine(sortedList1.IndexOfKey(key1))
Console.WriteLine(sortedList1.IndexOfKey(key2))
Console.WriteLine(sortedList1.IndexOfKey(key3))
Console.WriteLine()
Console.WriteLine(sortedList1.IndexOfValue(value1))
Console.WriteLine(sortedList1.IndexOfValue(value2))
Console.WriteLine(sortedList1.IndexOfValue(value3))
Console.WriteLine()
Console.WriteLine(sortedList1.Count)
sortedList1.Remove(key3)
Console.WriteLine(sortedList1.ContainsKey(key1))
Console.WriteLine(sortedList1.Count)
Console.ReadLine()
End Sub End ClassEnd NamespaceDit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.