473,915 Members | 7,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons;
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 IeDictionarywit h the desired number of items.
// The number of items cannot change for the life time of this
SimpleDictionar y.
public IeDictionary(In t32 numItems)
{
items = new DictionaryEntry[numItems];
}
#region IDictionary Members
public bool IsReadOnly { get { return false; } }
public bool Contains(object key)
{
Int32 index;
return TryGetIndexOfKe y(key, out index);
}
public bool IsFixedSize { get { return false; } }
public void Remove(object key)
{
if (key == null) throw new ArgumentNullExc eption("key");
// Try to find the key in the DictionaryEntry array
Int32 index;
if (TryGetIndexOfK ey(key, out index))
{
// If the key is found, slide all the items up.
Array.Copy(item s, 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 InvalidOperatio nException("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 (TryGetIndexOfK ey(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 (TryGetIndexOfK ey(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 TryGetIndexOfKe y(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\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.IDictionar y.GetEnumerator ()'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.CopyTo(System .Array, int)'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.Count'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.SyncRoot'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.IsSynchronize d'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.IEnumerabl e.GetEnumerator ()'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
Nov 17 '05 #1
2 3430
jg,

If you are using VS.NET 2005, why not just use:

Dictionary<obje ct, 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.co m

"jg" <ju**@mail.pl s> wrote in message
news:OI******** ******@TK2MSFTN GP14.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.Collecti ons;
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 IeDictionarywit h the desired number of items.
// The number of items cannot change for the life time of this
SimpleDictionar y.
public IeDictionary(In t32 numItems)
{
items = new DictionaryEntry[numItems];
}
#region IDictionary Members
public bool IsReadOnly { get { return false; } }
public bool Contains(object key)
{
Int32 index;
return TryGetIndexOfKe y(key, out index);
}
public bool IsFixedSize { get { return false; } }
public void Remove(object key)
{
if (key == null) throw new ArgumentNullExc eption("key");
// Try to find the key in the DictionaryEntry array
Int32 index;
if (TryGetIndexOfK ey(key, out index))
{
// If the key is found, slide all the items up.
Array.Copy(item s, 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 InvalidOperatio nException("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 (TryGetIndexOfK ey(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 (TryGetIndexOfK ey(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 TryGetIndexOfKe y(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\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.IDictionar y.GetEnumerator ()'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.CopyTo(System .Array, int)'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.Count'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.SyncRoot'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.IsSynchronize d'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.IEnumerabl e.GetEnumerator ()'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.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.c om> wrote in
message news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
jg,

If you are using VS.NET 2005, why not just use:

Dictionary<obje ct, 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.co m

"jg" <ju**@mail.pl s> wrote in message
news:OI******** ******@TK2MSFTN GP14.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.Collecti ons;
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 IeDictionarywit h the desired number of items.
// The number of items cannot change for the life time of this
SimpleDictionar y.
public IeDictionary(In t32 numItems)
{
items = new DictionaryEntry[numItems];
}
#region IDictionary Members
public bool IsReadOnly { get { return false; } }
public bool Contains(object key)
{
Int32 index;
return TryGetIndexOfKe y(key, out index);
}
public bool IsFixedSize { get { return false; } }
public void Remove(object key)
{
if (key == null) throw new ArgumentNullExc eption("key");
// Try to find the key in the DictionaryEntry array
Int32 index;
if (TryGetIndexOfK ey(key, out index))
{
// If the key is found, slide all the items up.
Array.Copy(item s, 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 InvalidOperatio nException("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 (TryGetIndexOfK ey(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 (TryGetIndexOfK ey(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 TryGetIndexOfKe y(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\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.IDictionar y.GetEnumerator ()'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.CopyTo(System .Array, int)'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.Count'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.SyncRoot'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.ICollectio n.IsSynchronize d'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.dll: (Location of
symbol
related to previous error)
c:\test\IeDicti ionary.cs(6,14) : error CS0535:
'IeDictionary' does not implement interface member
'System.Collect ions.IEnumerabl e.GetEnumerator ()'
c:\windows\Micr osoft.NET\Frame work\v2.0.50215 \mscorlib.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
2952
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, right? When I get to a certain item in the querystring, all items after that are to be put into the SECOND dictionary object.
9
10015
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 MyMsg.Count Response.Write(MyMsg.Item(i)) next
4
6181
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 nDBKey As Long) As ContentBlock Get
1
4529
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 'ContentObjects.ContentBlock'." The error occurs at the "For...Each" line if this method:
7
6151
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 since that would cause an exception in the foreach loop "foreach can not continue because the colelction was modifed". Now what is the least expensive and threadsafe way to make sure that no other threads modifies that collection. Since one of the...
7
57574
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 without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
2
10713
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
2006
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 page. I want to use something like a repeater or something like that. I don't know if this would be code I will have to manually write myself, or if it can be done with dataBinding of some sort. I am using vs2008 and c# 3.5. Any ideas on how to...
2
3147
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(); StockContract.Dictionary = ContractDictionary<string, string>(); GridView1.DataSource=StockContract.Dictionary; So far so good. Now I assign something to the Dictionary collection through some textboxes and a button:
2
3221
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: List<stringParts = new List<string>(); If a part number is not already in the list, a new entry is created for an employee that gets credited this part number:
0
10039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11359
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5944
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4779
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4346
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3370
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.