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 3393
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: boohoo |
last post by:
I can't seem to do this:
I want to take a query string and place two halves of the querystring
into two separate dictionary objects.
So... I loop through the collection of querystring items,...
|
by: What-a-Tool |
last post by:
Dim MyMsg
Set MyMsg = server.createObject("Scripting.Dictionary")
MyMsg.Add "KeyVal1", "My Message1"
MyMsg.Add "KeyVal2", "My Message2"
MyMsg.Add "KeyVal3", "My Message3"
for i = 1 To...
|
by: Martin Widmer |
last post by:
Hi folks.
I am using this collection class:
Public Class ContentBlocksCollection
Inherits DictionaryBase
'Object variables for attributes
'Attributes
Default Public Property Item(ByVal...
|
by: Martin Widmer |
last post by:
Hi Folks.
When I iterate through my custom designed collection, I always get the
error:
"Unable to cast object of type 'System.Collections.DictionaryEntry' to type...
|
by: bonk |
last post by:
Hello I am acessing a Dictionary<TKey,TValuefrom multiple threads and
often in a foreach loop. While I am within one of the foreach loops the
other threads must not modify the collection itself...
|
by: Andrew Robinson |
last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v>
depending on input parameters and options to the method.
1. Is there any way to convert from a dictionary to a list...
|
by: Stephen Costanzo |
last post by:
I have:
Public Class test
Private displayValue As String
Private dbType As Integer
Property Display() As String
Get
Return displayValue
End Get
|
by: Andy B |
last post by:
I have the object property StockContract.Dictionary which is a dictionary
collection of <string, stringkey/value pairs. I need to be able to
retreive the keys and their values and display them on a...
|
by: Andy B |
last post by:
I don't know if this is even working or not but here is the problem. I have
a gridview that I databound to a dictionary<string, stringcollection:
Contract StockContract = new Contract();...
|
by: =?Utf-8?B?anAybXNmdA==?= |
last post by:
I'm pulling records from or database by dates, and there are multiple records
for each part number.
The part numbers are being stored as strings in a List in a custom Employee
class:
...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |