KeyedCollection(TKey, TItem) is the abstract base class for a collection whose keys are embedded in the values. : HashSet « Data Structure « VB.Net

KeyedCollection(TKey, TItem) is the abstract base class for a collection whose keys are embedded in the values.


Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class SimpleOrder
    Inherits KeyedCollection(Of Integer, Product)
    Public Sub New()
    End Sub
    Protected Overrides Function GetKeyForItem( _
        ByVal item As Product) As Integer
        Return item.PartNumber   
    End Function

End Class

Public Class Demo
    Public Shared Sub Main() 
        Dim weekly As New SimpleOrder()
        weekly.Add(New Product(1, "A", 4, 4.7))
        weekly.Add(New Product(2, "B", 2, 5.3))
        weekly.Add(New Product(3, "C", 1, 2.5))
        weekly.Add(New Product(4, "D", 1, 5.17))



        weekly.Insert(2, New Product(5, "E", 10, .5))

        Dim coweekly As Collection(Of Product) = weekly


        coweekly(2) = New Product(6, "F", 27, 5.98)

        Dim temp As Product = coweekly(2)


    End Sub
    Private Shared Sub Display(ByVal order As SimpleOrder) 
        For Each item As Product In  order
        Next item
    End Sub
End Class

Public Class Product
    Public ReadOnly PartNumber As Integer
    Public ReadOnly Description As String
    Public ReadOnly UnitPrice As Double

    Private _quantity As Integer = 0

    Public Sub New(ByVal partNumber As Integer, _
                   ByVal description As String, _
                   ByVal quantity As Integer, _
                   ByVal unitPrice As Double) 
        Me.PartNumber = partNumber
        Me.Description = description
        Me.Quantity = quantity
        Me.UnitPrice = unitPrice
    End Sub 'New

    Public Property Quantity() As Integer 
            Return _quantity
        End Get
            If value < 0 Then
                Throw New ArgumentException("Quantity cannot be negative.")
            End If
            _quantity = value
        End Set
    End Property

    Public Overrides Function ToString() As String 
        Return String.Format(Description)
    End Function
End Class


Related examples in the same category

1.Lookup(TKey, TElement) Represents a collection of keys each mapped to one or more values.
2.ReadOnlyCollection(T) Class Provides the base class for a generic read-only collection.
3.HashSet(T) Class Represents a set of values.
4.Create a HashSet from another HashSet
5.Create HashSet(Of T) class that uses the default equality comparer for the set type
6.Create HashSet(Of T) class from another collection
7.Remove element from HashSet with condition function
8.HashSet(T).Clear Method removes all elements from a HashSet(Of T) object.
9.HashSet(T).Contains Method tells whether a HashSet(Of T) object contains the specified element.
10.HashSet(T).ExceptWith Method removes all elements in the specified collection from the current HashSet(Of T) object.
11.HashSet(T).IsProperSubsetOf Method tells whether a HashSet(Of T) object is a proper subset of the specified collection.
12.HashSet(T).SymmetricExceptWith Method keeps elements that are present either in that object or in the specified collection, but not both.