Click here to Skip to main content
Licence CPOL
First Posted 31 Jul 2007
Views 22,188
Bookmarked 13 times

Generic NameValueCollection Implementation

By | 31 Jul 2007 | Article
Generic NameValueCollection Implementation

Introduction

Sometimes Hashtable and generic Dictionary are simply not enough. They don't provide us with a getter via index. NameValueCollection allows us to create a list and reference items inside it by position or by value - but it has a limitation: keys and values must be strings.

Since I haven't found a GenericNameValue collection in .NET, I decided to create one.

Background

In .NET, there's an abstract NameObjectCollectionBase class, which has almost all the stuff I needed. To get rid of coding the class for each type, I decided to use Generics.

Using the Code

The code is quite simple. It uses generics.

Usage

private GenericNameValueCollection<MyClass> coll = 
		new GenericNameValueCollection<MyClass>();
/// <summary>
/// A generic NameValueCollection.
/// </summary>
/// <typeparam name="valueT">Value type.</typeparam>
public class GenericNameValueCollection <valueT> : NameObjectCollectionBase
{
  /// <summary>
  /// Creates an empty collection.
  /// </summary>
  public GenericNameValueCollection()
  {

  }

  /// <summary>
  /// Creates a collection from the IDictionary elements.
  /// </summary>
  /// <param name="dic">IDictionary object.</param>
  /// <param name="readOnly">Use TRUE to create a read-only collection.</param>

  public GenericNameValueCollection(IDictionary dic, bool readOnly)
  {
    foreach (DictionaryEntry de in dic)
    {
      this.BaseAdd(de.Key.ToString(), de.Value);
    }

    this.IsReadOnly = readOnly;
  }

  /// <summary>
  /// Gets a value using an index.
  /// </summary>
  /// <param name="index"></param>
  /// <returns></returns>
  public valueT this[int index]
  {
    get { return (valueT) this.BaseGet(index); }
  }

  /// <summary>
  /// Gets or sets a value for the given key.
  /// </summary>
  /// <param name="key"></param>
  /// <returns></returns>
  public valueT this[string key]
  {
    get { return (valueT) this.BaseGet(key); }
    set { this.BaseSet(key, value); }
  }

  /// <summary>
  /// Gets an array containing all the keys in the collection.
  /// </summary>
  public string[] AllKeys
  {
    get { return (string[])this.BaseGetAllKeys(); }
  }

  /// <summary>
  /// Gets an object array that contains all the values in the collection.
  /// </summary>
  public object[] AllValues
  {
    get { return this.BaseGetAllValues(); }
  }

  /// <summary>
  /// Gets a value indicating if the collection contains keys that are not null.
  /// </summary>
  public Boolean HasKeys
  {
    get { return this.BaseHasKeys(); }
  }

  /// <summary>
  /// Adds an entry to the collection.
  /// </summary>
  /// <param name="key"></param>
  /// <param name="value"></param>
  public void Add(string key, valueT value)
  {
    this.BaseAdd(key, value);
  }

  /// <summary>
  /// Removes an entry with the specified key from the collection.
  /// </summary>
  /// <param name="key"></param>
  public void Remove(string key)
  {
    this.BaseRemove(key);
  }

  /// <summary>
  /// Removes an entry in the specified index from the collection.
  /// </summary>
  /// <param name="index"></param>
  public void Remove(int index)
  {
    this.BaseRemoveAt(index);
  }

  /// <summary>
  /// Clears all the elements in the collection.
  /// </summary>
  public void Clear()
  {
    this.BaseClear();
  }
}

History

  • 1st August, 2007: Initial post

License

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

About the Author

balazs_hideghety

Web Developer

Slovakia Slovakia

Member

Since 1999 I work in IT. Worked 2-3 yrs with Borland Builder C++. Since .NET appeared, I program in C#, ASP.NET.
 
You may know the current technologies, but still there's a lot of experience to gain. IT's evolving all the time.
 
From 2006 I'm a MCP. Now I'm focusing on technologies like: NHibernate, NSpring...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberetclermont1:48 25 Aug '09  
GeneralKeyedCollection PinmemberPaulo Zemek9:38 6 Jul '09  
GeneralTotally wrong PinmemberYllusio22:12 7 Jul '08  
GeneralElegant and useful solution PinmemberEclecticDog4:36 26 Jun '08  
GeneralExactly what I was looking for... PinmemberAlexander Gräf3:58 27 Sep '07  
GeneralDot Net already contains these collections PinmemberNeal Andrews9:42 3 Aug '07  
GeneralRe: Dot Net already contains these collections Pinmemberbalazs_hideghety0:30 5 Aug '07  
GeneralRe: Dot Net already contains these collections PinmemberNeal Andrews0:48 5 Aug '07  
GeneralRe: Dot Net already contains these collections Pinmemberbalazs_hideghety8:55 5 Aug '07  
GeneralUseless Pinmemberstevekain5:17 1 Aug '07  
GeneralRe: Useless Pinmemberbalazs_hideghety20:47 1 Aug '07  
GeneralDictionary PinmemberRexNebular23:13 31 Jul '07  
GeneralRe: Dictionary Pinmemberbalazs_hideghety23:39 31 Jul '07  
GeneralRe: Dictionary PinmemberMichael Sync23:44 31 Jul '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 1 Aug 2007
Article Copyright 2007 by balazs_hideghety
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid