I want to implement a priority queue class. When an item is added at a higher priority it is pushed to the front of the queue instead adding to the end of queue.
Simple few lines of code
Public Class PriorityQueue(Of T)
Inherits List(Of T)
Private _list As New List(Of T)
Public Sub Enque(ByVal item As T, Optional ByVal pushToFront As Boolean = False)
If pushToFront = True Then
_list.Insert(0, item)
Else
_list.Add(item)
End If
End Sub
Public Function Deque() As T
If _list.Count <> 0 Then
Dim item As T = _list(0)
_list.RemoveAt(0)
Return item
Else
Throw New InvalidOperationException
End If
End Function
End Class
Now the the calling function tries to find the elements in the queue thus ....
dim _q as new PriorityQueue(Of integer)
_q.Enque(1)
_q.Enque(2)
msgbox(_q.Count())
.....
the program prints out 0! If add a Count() property then everything is fine. I would have thought the inherited class should call the base class's Count function. Note that the Count shows up in intellisense even if I have no implementation in the derived class.