473,249 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,249 software developers and data experts.

dictionary or collection

jg
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)
Nov 17 '05 #1
2 3386
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)

Nov 17 '05 #2
gg
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)


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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,...
9
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...
4
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...
1
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...
7
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...
7
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...
2
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
8
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...
2
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();...
2
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: ...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.