Visual Basic 2012 Voorbeelden
   

visual basic 2012 broncode voorbeelden

Blijf op de hoogte van de recente aanpassingen op vbvoorbeelden!

Microsoft Visual Studio 2012Microsoft Developers Network - Visual BasicMicrosoft .NET Framework

22.20. SortedList Item, Contains(Key) en ContainsValue

Print Email Deel op Twitter Deel op Facebook

Dit artikel is gepubliceerd op maandag 15 oktober 2012 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.

Methods als Add, Remove, Contains, ContainsKey, IndexOfKey en Item van de SortedList klasse werken op basis van de CompareTo implementatie van de keys van de elementen.
Zo levert in onderstaand voorbeeld de expressie sortedList1.Item(key3) "value1" op omdat key1.CompareTo(key3) nul oplevert (1).  En zo zal bijvoorbeeld sortedList1.Remove(key3) om dezelfde reden de entry met key1 verwijderen (2).

De ContainsValue en IndexOfValue methods baseren zich uiteraard op de Equals implementatie van de values van de elementen :
Visual Basic 2012 Broncode - Codevoorbeeld 581
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))        ' True
            Console.WriteLine(sortedList1.Contains(key2))        ' False
            Console.WriteLine(sortedList1.Contains(key3))        ' True
            Console.WriteLine()
            '
            Console.WriteLine(sortedList1.ContainsKey(key1))     ' True
            Console.WriteLine(sortedList1.ContainsKey(key2))     ' False
            Console.WriteLine(sortedList1.ContainsKey(key3))     ' True
            Console.WriteLine()
            '
            Console.WriteLine(sortedList1.ContainsValue(value1)) ' True
            Console.WriteLine(sortedList1.ContainsValue(value2)) ' True
            Console.WriteLine(sortedList1.ContainsValue(value3)) ' False
            Console.WriteLine()
            '
            Console.WriteLine(sortedList1.Item(key1).ToString()) ' value1
            Console.WriteLine(sortedList1.Item(key2) Is Nothing) ' True
            Console.WriteLine(sortedList1.Item(key3).ToString()) ' value1    (1)
            Console.WriteLine()
            '
            Console.WriteLine(sortedList1.IndexOfKey(key1))      ' 0
            Console.WriteLine(sortedList1.IndexOfKey(key2))      ' -1
            Console.WriteLine(sortedList1.IndexOfKey(key3))      ' 0
            Console.WriteLine()
            '
            Console.WriteLine(sortedList1.IndexOfValue(value1))  ' 0
            Console.WriteLine(sortedList1.IndexOfValue(value2))  ' 0
            Console.WriteLine(sortedList1.IndexOfValue(value3))  ' -1
            Console.WriteLine()
            '
            Console.WriteLine(sortedList1.Count)                 ' 1
            '
            sortedList1.Remove(key3)                                       ' (2)
            '
            Console.WriteLine(sortedList1.ContainsKey(key1))     ' False
            Console.WriteLine(sortedList1.Count)                 ' 0
            '
            Console.ReadLine()
        End Sub
    End Class
End Namespace
Console Application Output
True
False
True

True
False
True

True
True
False

value1
True
value1

0
-1
0

0
0
-1

1
False
0

Dit artikel is gepubliceerd op maandag 15 oktober 2012 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.