Click here to Skip to main content
12,406,592 members (53,551 online)
Click here to Skip to main content
Add your own
alternative version

Stats

29.8K views
13 bookmarked
Posted

Generic NameValueCollection Implementation

, 31 Jul 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

balazs_hideghety
Web Developer
Slovakia Slovakia
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...

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
etclermont25-Aug-09 1:48
memberetclermont25-Aug-09 1:48 
GeneralKeyedCollection Pin
Paulo Zemek6-Jul-09 9:38
memberPaulo Zemek6-Jul-09 9:38 
GeneralTotally wrong Pin
Yllusio7-Jul-08 22:12
memberYllusio7-Jul-08 22:12 
GeneralElegant and useful solution Pin
EclecticDog26-Jun-08 4:36
memberEclecticDog26-Jun-08 4:36 
GeneralExactly what I was looking for... Pin
Alexander Gräf27-Sep-07 3:58
memberAlexander Gräf27-Sep-07 3:58 
GeneralDot Net already contains these collections Pin
Neal Andrews3-Aug-07 9:42
memberNeal Andrews3-Aug-07 9:42 
GeneralRe: Dot Net already contains these collections Pin
balazs_hideghety5-Aug-07 0:30
memberbalazs_hideghety5-Aug-07 0:30 
GeneralRe: Dot Net already contains these collections Pin
Neal Andrews5-Aug-07 0:48
memberNeal Andrews5-Aug-07 0:48 
GeneralRe: Dot Net already contains these collections Pin
balazs_hideghety5-Aug-07 8:55
memberbalazs_hideghety5-Aug-07 8:55 
GeneralUseless Pin
stevekain1-Aug-07 5:17
memberstevekain1-Aug-07 5:17 
GeneralRe: Useless Pin
balazs_hideghety1-Aug-07 20:47
memberbalazs_hideghety1-Aug-07 20:47 
GeneralDictionary Pin
RexNebular31-Jul-07 23:13
memberRexNebular31-Jul-07 23:13 
GeneralRe: Dictionary Pin
balazs_hideghety31-Jul-07 23:39
memberbalazs_hideghety31-Jul-07 23:39 
GeneralRe: Dictionary Pin
Michael Sync31-Jul-07 23:44
memberMichael Sync31-Jul-07 23:44 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160730.1 | Last Updated 1 Aug 2007
Article Copyright 2007 by balazs_hideghety
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid