Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is what I am thinking is my best solution. I welcome comments for a better way!</p> <pre><code>Imports Microsoft.VisualBasic Public Class Car Implements Xs(Of Car).IKeyName Private _KeyName As String Public Sub New(ByVal keyName As String, ByVal property1 As String) _KeyName = keyName _Property1 = property1 End Sub Public Property KeyName() As String Implements Xs(Of Car).IKeyName.KeyName Get Return _KeyName End Get Set(ByVal value As String) _KeyName = value End Set End Property Public _Property1 As String Public Property Property1() As String Get Return _Property1 End Get Set(ByVal value As String) _Property1 = value End Set End Property End Class Public Class Cars Inherits System.ComponentModel.BindingList(Of Car) Public Overloads ReadOnly Property Item(ByVal key As String) As Car Get For Each CurrentCar As Car In Me.Items If CurrentCar.KeyName = key Then Return CurrentCar End If Next Return Nothing End Get End Property End Class Public Class X Private _KeyName As String Public Property KeyName() As String Get Return _Keyname End Get Set(ByVal value As String) _Keyname = value End Set End Property End Class Public Class Xs(Of X) Inherits Hashtable Interface IKeyName Property KeyName() As String End Interface Public Shadows Sub Add(ByVal item As IKeyName) MyBase.Add(item.KeyName, item) End Sub Public Shadows ReadOnly Property Item(ByVal key As String) As x Get If Me.ContainsKey(key) Then Return MyBase.Item(key) Else 'If I mispell a key, I don't want to end up creating a new mispelled version, I want an error Throw New Exception("Element with key " &amp; key &amp; " is not found") End If End Get End Property End Class Public Class Cars2 Inherits Xs(Of Car) End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim MyCar As New Car("key1", "prop1") 'First approach Dim MyCars As New Cars MyCars.Add(MyCar) Dim RetrievedCar As Car = MyCars.Item("key1") 'Inefficient retrieval by key (uses looping) 'Second approach Dim Cars2 As New Cars2 Cars2.Add(MyCar) Dim RetrievedCar2 As Car = Cars2.Item("key1") 'Can now efficiently retreive an item by its key End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload