I was trying to get custom dictionary class that can store generic or
string[]; So I started with the example given by the visual studio 2005 c#
online help for simpledictionay object
That seem to miss a few things including #endregion directive and the ending
class }
Is there not a simple way like in dotnet vb?
I managed to get the sample to code to this:
using System;
using System.Collections;
using Microsoft.Win32;
// This class implements a simple dictionary using an array of
DictionaryEntry objects (key/value pairs).
public class IeDictionary: IDictionary
{
// The array of items
private DictionaryEntry[] items;
private Int32 ItemsInUse = 0;
// Construct the IeDictionarywith the desired number of items.
// The number of items cannot change for the life time of this
SimpleDictionary.
public IeDictionary(Int32 numItems)
{
items = new DictionaryEntry[numItems];
}
#region IDictionary Members
public bool IsReadOnly { get { return false; } }
public bool Contains(object key)
{
Int32 index;
return TryGetIndexOfKey(key, out index);
}
public bool IsFixedSize { get { return false; } }
public void Remove(object key)
{
if (key == null) throw new ArgumentNullException("key");
// Try to find the key in the DictionaryEntry array
Int32 index;
if (TryGetIndexOfKey(key, out index))
{
// If the key is found, slide all the items up.
Array.Copy(items, index + 1, items, index, ItemsInUse - index -
1);
ItemsInUse--;
}
else
{
// If the key is not in the dictionary, just return.
}
}
public void Clear() { ItemsInUse = 0; }
public void Add(object key, object value)
{
// Add the new key/value pair even if this key already exists in the
dictionary.
if (ItemsInUse == items.Length)
throw new InvalidOperationException("The dictionary cannot hold
any more items.");
items[ItemsInUse++] = new DictionaryEntry(key, value);
}
public ICollection Keys
{
get
{
// Return an array where each item is a key.
Object[] keys = new Object[ItemsInUse];
for (Int32 n = 0; n < ItemsInUse; n++)
keys[n] = items[n].Key;
return keys;
}
}
public ICollection Values
{
get
{
// Return an array where each item is a value.
Object[] values = new Object[ItemsInUse];
for (Int32 n = 0; n < ItemsInUse; n++)
values[n] = items[n].Value;
return values;
}
}
public object this[object key]
{
get
{
// If this key is in the dictionary, return its value.
Int32 index;
if (TryGetIndexOfKey(key, out index))
{
// The key was found; return its value.
return items[index].Value;
}
else
{
// The key was not found; return null.
return null;
}
}
set
{
// If this key is in the dictionary, change its value.
Int32 index;
if (TryGetIndexOfKey(key, out index))
{
// The key was found; change its value.
items[index].Value = value;
}
else
{
// This key is not in the dictionary; add this key/value
pair.
Add(key, value);
}
}
}
private Boolean TryGetIndexOfKey(Object key, out Int32 index)
{
for (index = 0; index < ItemsInUse; index++)
{
// If the key is found, return true (the index is also
returned).
if (items[index].Key.Equals(key)) return true;
}
// Key not found, return false (index should be ignored by the
caller).
return false;
}
#endregion
}
But I got alot erors. Mostly on those I don't plant to use
c:\test\IeDictiionary.cs(6,14): error CS0535:
'IeDictionary' does not implement interface member
'System.Collections.IDictionary.GetEnumerator()'
c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDictiionary.cs(6,14): error CS0535:
'IeDictionary' does not implement interface member
'System.Collections.ICollection.CopyTo(System.Arra y, int)'
c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDictiionary.cs(6,14): error CS0535:
'IeDictionary' does not implement interface member
'System.Collections.ICollection.Count'
c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDictiionary.cs(6,14): error CS0535:
'IeDictionary' does not implement interface member
'System.Collections.ICollection.SyncRoot'
c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDictiionary.cs(6,14): error CS0535:
'IeDictionary' does not implement interface member
'System.Collections.ICollection.IsSynchronized'
c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDictiionary.cs(6,14): error CS0535:
'IeDictionary' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'
c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of
symbol
related to previous error) 2 3207
jg,
If you are using VS.NET 2005, why not just use:
Dictionary<object, object>
Or a Hashtable?
As for your compiler errors, the interfaces you are implementing derive
from other interfaces. You have to implement those members as well.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"jg" <ju**@mail.pls> wrote in message
news:OI**************@TK2MSFTNGP14.phx.gbl... I was trying to get custom dictionary class that can store generic or string[]; So I started with the example given by the visual studio 2005 c# online help for simpledictionay object
That seem to miss a few things including #endregion directive and the ending class }
Is there not a simple way like in dotnet vb?
I managed to get the sample to code to this: using System; using System.Collections; using Microsoft.Win32;
// This class implements a simple dictionary using an array of DictionaryEntry objects (key/value pairs). public class IeDictionary: IDictionary { // The array of items private DictionaryEntry[] items; private Int32 ItemsInUse = 0;
// Construct the IeDictionarywith the desired number of items. // The number of items cannot change for the life time of this SimpleDictionary. public IeDictionary(Int32 numItems) { items = new DictionaryEntry[numItems]; }
#region IDictionary Members public bool IsReadOnly { get { return false; } } public bool Contains(object key) { Int32 index; return TryGetIndexOfKey(key, out index); } public bool IsFixedSize { get { return false; } } public void Remove(object key) { if (key == null) throw new ArgumentNullException("key"); // Try to find the key in the DictionaryEntry array Int32 index; if (TryGetIndexOfKey(key, out index)) { // If the key is found, slide all the items up. Array.Copy(items, index + 1, items, index, ItemsInUse - index - 1); ItemsInUse--; } else { // If the key is not in the dictionary, just return. } } public void Clear() { ItemsInUse = 0; } public void Add(object key, object value) { // Add the new key/value pair even if this key already exists in the dictionary. if (ItemsInUse == items.Length) throw new InvalidOperationException("The dictionary cannot hold any more items."); items[ItemsInUse++] = new DictionaryEntry(key, value); } public ICollection Keys { get { // Return an array where each item is a key. Object[] keys = new Object[ItemsInUse]; for (Int32 n = 0; n < ItemsInUse; n++) keys[n] = items[n].Key; return keys; } } public ICollection Values { get { // Return an array where each item is a value. Object[] values = new Object[ItemsInUse]; for (Int32 n = 0; n < ItemsInUse; n++) values[n] = items[n].Value; return values; } } public object this[object key] { get { // If this key is in the dictionary, return its value. Int32 index; if (TryGetIndexOfKey(key, out index)) { // The key was found; return its value. return items[index].Value; } else { // The key was not found; return null. return null; } }
set { // If this key is in the dictionary, change its value. Int32 index; if (TryGetIndexOfKey(key, out index)) { // The key was found; change its value. items[index].Value = value; } else { // This key is not in the dictionary; add this key/value pair. Add(key, value); } } } private Boolean TryGetIndexOfKey(Object key, out Int32 index) { for (index = 0; index < ItemsInUse; index++) { // If the key is found, return true (the index is also returned). if (items[index].Key.Equals(key)) return true; }
// Key not found, return false (index should be ignored by the caller). return false; } #endregion }
But I got alot erors. Mostly on those I don't plant to use c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.IDictionary.GetEnumerator()' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error) c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.ICollection.CopyTo(System.Arra y, int)' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error) c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.ICollection.Count' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error) c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.ICollection.SyncRoot' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error) c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.ICollection.IsSynchronized' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error) c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error)
thanks. I will try Dictionary.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl... jg,
If you are using VS.NET 2005, why not just use:
Dictionary<object, object>
Or a Hashtable?
As for your compiler errors, the interfaces you are implementing derive from other interfaces. You have to implement those members as well.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"jg" <ju**@mail.pls> wrote in message news:OI**************@TK2MSFTNGP14.phx.gbl...I was trying to get custom dictionary class that can store generic or string[]; So I started with the example given by the visual studio 2005 c# online help for simpledictionay object
That seem to miss a few things including #endregion directive and the ending class }
Is there not a simple way like in dotnet vb?
I managed to get the sample to code to this: using System; using System.Collections; using Microsoft.Win32;
// This class implements a simple dictionary using an array of DictionaryEntry objects (key/value pairs). public class IeDictionary: IDictionary { // The array of items private DictionaryEntry[] items; private Int32 ItemsInUse = 0;
// Construct the IeDictionarywith the desired number of items. // The number of items cannot change for the life time of this SimpleDictionary. public IeDictionary(Int32 numItems) { items = new DictionaryEntry[numItems]; }
#region IDictionary Members public bool IsReadOnly { get { return false; } } public bool Contains(object key) { Int32 index; return TryGetIndexOfKey(key, out index); } public bool IsFixedSize { get { return false; } } public void Remove(object key) { if (key == null) throw new ArgumentNullException("key"); // Try to find the key in the DictionaryEntry array Int32 index; if (TryGetIndexOfKey(key, out index)) { // If the key is found, slide all the items up. Array.Copy(items, index + 1, items, index, ItemsInUse - index - 1); ItemsInUse--; } else { // If the key is not in the dictionary, just return. } } public void Clear() { ItemsInUse = 0; } public void Add(object key, object value) { // Add the new key/value pair even if this key already exists in the dictionary. if (ItemsInUse == items.Length) throw new InvalidOperationException("The dictionary cannot hold any more items."); items[ItemsInUse++] = new DictionaryEntry(key, value); } public ICollection Keys { get { // Return an array where each item is a key. Object[] keys = new Object[ItemsInUse]; for (Int32 n = 0; n < ItemsInUse; n++) keys[n] = items[n].Key; return keys; } } public ICollection Values { get { // Return an array where each item is a value. Object[] values = new Object[ItemsInUse]; for (Int32 n = 0; n < ItemsInUse; n++) values[n] = items[n].Value; return values; } } public object this[object key] { get { // If this key is in the dictionary, return its value. Int32 index; if (TryGetIndexOfKey(key, out index)) { // The key was found; return its value. return items[index].Value; } else { // The key was not found; return null. return null; } }
set { // If this key is in the dictionary, change its value. Int32 index; if (TryGetIndexOfKey(key, out index)) { // The key was found; change its value. items[index].Value = value; } else { // This key is not in the dictionary; add this key/value pair. Add(key, value); } } } private Boolean TryGetIndexOfKey(Object key, out Int32 index) { for (index = 0; index < ItemsInUse; index++) { // If the key is found, return true (the index is also returned). if (items[index].Key.Equals(key)) return true; }
// Key not found, return false (index should be ignored by the caller). return false; } #endregion }
But I got alot erors. Mostly on those I don't plant to use c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.IDictionary.GetEnumerator()' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error) c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.ICollection.CopyTo(System.Arra y, int)' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error) c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.ICollection.Count' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error) c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.ICollection.SyncRoot' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error) c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.ICollection.IsSynchronized' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error) c:\test\IeDictiionary.cs(6,14): error CS0535: 'IeDictionary' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()' c:\windows\Microsoft.NET\Framework\v2.0.50215\msco rlib.dll: (Location of symbol related to previous error)
This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by boohoo |
last post: by
|
9 posts
views
Thread by What-a-Tool |
last post: by
|
4 posts
views
Thread by Martin Widmer |
last post: by
|
1 post
views
Thread by Martin Widmer |
last post: by
|
7 posts
views
Thread by bonk |
last post: by
|
7 posts
views
Thread by Andrew Robinson |
last post: by
|
2 posts
views
Thread by Stephen Costanzo |
last post: by
|
8 posts
views
Thread by Andy B |
last post: by
|
2 posts
views
Thread by Andy B |
last post: by
|
2 posts
views
Thread by =?Utf-8?B?anAybXNmdA==?= |
last post: by
| | | | | | | | | | |