I have some custom classes and collections for those classes. I'm kind of new to actually implementing all of this, and I see a lot of different options but all with their own pros and cons, and I'm having trouble determining the best/right way to do what I need.
For example, I have a Product class with ID and Description properties. I have a ProductCollection class that I basically want to be a dictionary consisting of Product objects. This is a dictionary because there is a performance gain in accessing by key value, which is how I'll reference the objects. The key is Product.ID, and the value is Product.
I'd like to be able to do something like ProductCollection.Add(Product), and the collection will handle signing the dictionary key from the object's ID property. How do I beat accomplish this? Implement Dictionary(Of integer, Product) and override basically all methods to pass in the object and property separately? Or is there a better way?
Is this the kind of thing you're looking for:
Imports System.Collections.ObjectModel Public Class Cars Public Key As String Public Sub New(ByVal make As String, ByVal model As String) _make = make _model = model End Sub Private _make As String Public ReadOnly Property Make() As String Get Return _make End Get End Property Private _model As String Public ReadOnly Property Model() As String Get Return _model End Get End Property End Class Public Class Cars Inherits KeyedCollection(Of String, Car) Protected Overrides Function GetKeyForItem(ByVal item As Car) As String Return item.Key End Function End Class
You would then use it like this:
Dim myCars as new Cars myCars.Add(New Car("Make","Model"))