title: How to implement/override Protected List.Contains() for a custom Collection Class using a property of item
body: <p>I'm not entirely sure that my wording is correct on the title above. I guess the easiest way to explain it is to put the code below for reference and then give what I'd like to have as the expected behavior. </p>
  
  
  
  <hr>
  
  <pre class="lang-vb prettyprint-override"><code>Public Class Domino
      Public ReadOnly MinimumSideValue As Integer = 0
      Public ReadOnly MaximumSideValue As Integer = 6
  
      Public Property key As Integer
      Public Property Side1 As Integer
      Public Property Side2 As Integer
  
      Public Sub New(side1Value As Integer, side2Value As Integer)
          Me.Side1 = side1Value
          Me.Side2 = side2Value
          ' Key should be a two digit number. side values 5 and 1 -&gt; 51
          Me.key = Integer.Parse(Side1.ToString &amp; Side2.ToString)
      End Sub
  
      Public Overrides Function ToString() As String
          Return Convert.ToString(Me.key)
      End Function
  
  End Class
  </code></pre>
  
  <hr>
  
  <pre class="lang-vb prettyprint-override"><code>Public Class DominoCollection
      Inherits System.Collections.CollectionBase
  
      Public Sub AddDomino(newDomino As Domino)
          ' Not sure if I should be calling Contains on the Protected List object
          If Not List.Contains(newDomino) Then
              List.Add(newDomino)
          End If    
      End Sub
  
      Public Function Contains(objDomino as Domino) As Boolean
          ' Need to check if a domino that has the same key already exists
          Dim found As Boolean = False
          For i = 0 To List.Count - 1 'This will fail if the list is empty...
              If DirectCast(List.Item(i), Domino).key = objDomino.key Then found = True
          Next
          Return found
      End Function    
  
  End Class
  </code></pre>
  
  <hr>
  
  <p>The fist code listing is the class that will have multiple instances created. The second class is the collection that will contain instances of the first class. The collection should not contain duplicate items. So when I call the contains method on the built-in protected <code>List</code> object from the CollectionBase I'd like it to search the list of items looking for a duplicate with the same value in the <code>key</code> property of the first class. </p>
  
  <p>I'm not entirely sure if I can override the List.Contains method because it's a protected object and/or if I should really be creating my own contains method. I tried creating my own .Contains method, but it fails if the list is empty.</p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>I know that my code isn't in C#. However since it's .NET, C# answers would help just as much as VB would.</p>
  
  <hr>
  
  <h2>EDIT: Revised Code</h2>
  
  <p>After seeing some of the solutions below I've got the following that so far is looking like it will work as I wanted. However I'm not sure I implemented the <code>Item</code> property correctly. I haven't tested it as of yet because I'm not to the point of being able to use it in the rest of the application, I'm just trying to get a framework down.</p>
  
  <pre class="lang-vb prettyprint-override"><code>Friend Class DominoCollection
      Private domCol As Dictionary(Of Integer, Domino)
  
      Public ReadOnly Property Keys As System.Collections.Generic.Dictionary(Of Integer, Domino).KeyCollection
          Get
              Return domCol.Keys
          End Get
      End Property
  
      Public ReadOnly Property Values As System.Collections.Generic.Dictionary(Of Integer, Domino).ValueCollection
          Get
              Return domCol.Values
          End Get
      End Property
  
      Default Public Property Item(DominoKey As Integer) As Domino
          Get
              If domCol.ContainsKey(DominoKey) Then
                  Return domCol.Item(DominoKey)
              Else
                  Throw New KeyNotFoundException(String.Format("Cannot find key {0} in internal collection"))
              End If
          End Get
          Set(value As Domino)
              If domCol.ContainsKey(DominoKey) Then
                  domCol.Item(DominoKey) = value
              Else
                  Throw New KeyNotFoundException(String.Format("Cannot find key {0} in internal collection"))
              End If
          End Set
      End Property
  
      Default Public Property Item(domino As Domino) As Domino
          Get
              If domCol.ContainsKey(domino.key) Then
                  Return domCol.Item(domino.key)
              Else
                  Throw New KeyNotFoundException(String.Format("Cannot find key {0} in internal collection"))
              End If
          End Get
          Set(value As Domino)
              If domCol.ContainsKey(domino.key) Then
                  domCol.Item(domino.key) = value
              Else
                  Throw New KeyNotFoundException(String.Format("Cannot find key {0} in internal collection"))
              End If
          End Set
      End Property
  
      Public Sub New()
          domCol = New Dictionary(Of Integer, Domino)
      End Sub
  
      Public Sub Add(dom As Domino)
          If Not domCol.ContainsKey(dom.key) Then
              domCol.Add(dom.key, dom)
          End If
      End Sub
  
      Public Function Contains(dom As Domino) As Boolean
          Return domCol.ContainsKey(dom.key)
      End Function
  
      ' flexibility:
      Public Function Remove(dom As Domino) As Boolean
          If domCol.ContainsKey(dom.key) Then
              domCol.Remove(dom.key)
              Return True
          Else
              Return False
          End If
      End Function
  
      Public Function Remove(key As Integer) As Boolean
          If domCol.ContainsKey(key) Then
              domCol.Remove(key)
              Return True
          Else
              Return False
          End If
      End Function
  
      Public Function Count() As Integer
          Return domCol.Count
      End Function
  
      Public Sub Clear()
          domCol.Clear()
      End Sub
  
  End Class
  </code></pre>