NameObjectCollectionBase

  • Thread starter William Stacey [MVP]
  • Start date
W

William Stacey [MVP]

What is the deal with NameObjectCollectionBase? It allows duplication keys
to be added to the hashtable

using System;
using System.Collections;
using System.Collections.Specialized;

namespace ns
{
/// <summary>
/// Summary description for HashListNO.
/// </summary>
public class HashListNO : NameObjectCollectionBase
{
#region Fields
private DictionaryEntry de = new DictionaryEntry();
private readonly object syncRoot = new object();
#endregion

#region Constructors
public HashListNO()
{
}
public HashListNO( IHashCodeProvider hashProvider, IComparer comparer ) :
base(hashProvider, comparer)
{
}
public HashListNO( IDictionary d, Boolean bReadOnly )
{
foreach ( DictionaryEntry de in d )
{
this.BaseAdd( (String) de.Key, de.Value );
}
this.IsReadOnly = bReadOnly;
}
#endregion

// Gets the syncRoot object for this collection.
public object SyncRoot
{
get { return this.syncRoot; }
}

// Gets a key-and-value pair (DictionaryEntry) using an index.
public DictionaryEntry this[ int index ]
{
get
{
de.Key = this.BaseGetKey(index);
de.Value = this.BaseGet(index);
return( de );
}
}

// Gets or sets the value associated with the specified key.
public Object this[ string key ]
{
get
{
return( this.BaseGet( key ) );
}
set
{
this.BaseSet( key, value );
}
}

// Gets a String array that contains all the keys in the collection.
public String[] AllKeys
{
get
{
return( this.BaseGetAllKeys() );
}
}

// Gets an Object array that contains all the values in the collection.
public Array AllValues
{
get
{
return( this.BaseGetAllValues() );
}
}

// Gets a String array that contains all the values in the collection.
public String[] AllStringValues
{
get
{
return( (string[]) this.BaseGetAllValues( Type.GetType(
"System.String" ) ) );
}
}

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

// Adds an entry to the collection.
public void Add( string key, object value )
{
this.BaseAdd( key, value );
}

// Removes an entry with the specified key from the collection.
public void Remove( string key )
{
this.BaseRemove( key );
}

// Removes an entry in the specified index from the collection.
public void RemoveAt( int index )
{
this.BaseRemoveAt( index );
}

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

William Stacey [MVP]

ahh. I see the error in the base class. BaseAdd() is totally wrong. Can't
believe that made it passed testing.
 
K

Kevin Yu [MSFT]

Hi William,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you were able to add entries with
duplicated keys into a NameObjectCollectionBase. If there is any
misunderstanding, please feel free to let me know.

As far as I know, this is by design. When adding entries with duplicated
keys, the newly added entry was actually not added to the hashtable. It was
put in another arraylist inside the object, which
NameObjectCollectionBase.BaseGet cannot get.

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
W

William Stacey [MVP]

I guess your right. It does talk about keys with same name. This seems
like strange behavior for a hashtable type of class that supports case
insensitive comparers. The class doco should call this out explicitly in
the main page of the class. Cheers!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top