Click here to Skip to main content
Click here to Skip to main content

Non-Repeatable Collections

, 4 Apr 2010
Rate this:
Please Sign up or sign in to vote.
Download source - 30 KBDownload library - 5 KBIntroductionThis set of Non-Repeatable Collections has a very simple concept - to only allow one item of each type to be added, meaning that you cannot have duplicate items in the collection.BackgroundAlthough the .NET Framework...
Download source - 30 KB
Download library - 5 KB
 

Introduction
 
This set of Non-Repeatable Collections has a very simple concept - to only allow one item of each type to be added, meaning that you cannot have duplicate items in the collection.
 
Background
 
Although the .NET Framework includes a HashSet(Of T) collection that does not allow repeated items, I figured that some developers may not want to switch to a collection such as the HashSet, but rather use ones that have the same functionality of common collections such as Lists and Stacks, which are specialised for certain tasks.
 
The idea for creating this set of 11 collections came to me while I was in the early stages of developing a music software program. I had to store musical notes in a List, but not allow the same note to be added twice.
 
Using the Code
 
To use the code, first add a reference to the NonRepeatableCollections.dll library. This is located in the Bin/Release folder.
 
Add these Imports statement as follows:
 
Imports NonRepeatableCollections
Imports NonRepeatableCollections.Generic
 
You can then use the collections as normal, for example:
 
Dim list As New NonRepeatableList(Of String)
list.Add("hello")
list.Add("goodbye")
list.Add("hello")
 
'List items are now "hello" and "goodbye"
 
How It Works
 
To create the non repeatable collections, I derived from the individual collection classes, shadowing the methods that added items to the collections, for example the Add method of the List(Of T) or the Enqueue method of the Queue.
 
In these shadowed methods, the code performs a check on the collection to make sure that the item(s) being added are not already contained. If they are already contained, the code discards the item, otherwise it proceeds to add it to the collection.
 
That's basically all there is to it, I also coded shadowed methods for other less obvious methods like the Insert method of the List(Of T), as this also adds item(s) to the collection. It performs the same check as with the Add method etc.
 
Current Collections
 
Here is a list of the collections currently in the library:
 
Normal
 
  • NonRepeatableArrayList
  • NonRepeatableHashtable
  • NonRepeatableQueue
  • NonRepeatableSortedList
  • NonRepeatableStack
 
Generic
 
  • NonRepeatableDictionary
  • NonRepeatableList
  • NonRepeatableQueue
  • NonRepeatableSortedDictionary
  • NonRepeatableSortedList
  • NonRepeatableStack

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Anthony Daly
Founder SoftwareStats
United Kingdom United Kingdom
I'm a software and web developer based in London, United Kingdom. I have experience in a variety of technologies, with a particular interest in Windows Forms applications, and PHP web applications. I am the founder of SoftwareStats, a Runtime Intelligence and Software Analytics service allowing .NET developers to track their applications.
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 4 Apr 2010
Article Copyright 2010 by Anthony Daly
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid