473,396 Members | 1,734 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,396 software developers and data experts.

Writing a Collection

Hi

I want to write my own collection object. I want it to have the following
features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The
indexer should return the first item that's ID property is equal to the
parameter. These collections will generally contain less than 10 items, so I
don't intend to use a HashTable.
I really don't know where to start. Dose any one know where I can find a
sample which dose something like this?

Gary
Nov 15 '05 #1
9 1660
Hi,

See the documentation and examples on the System.Collections.DictionaryBase
class. It uses a HashTable internally, but is very convenient for building
strong-typed dictionaries.

On the other hand, Hashtable can be optimized for different amounts of data
stored so there's no reason to worry about its performance.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Gary van der Merwe" <ga*****@hotmail.com> wrote in message
news:eC*************@tk2msftngp13.phx.gbl...
Hi

I want to write my own collection object. I want it to have the following
features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The
indexer should return the first item that's ID property is equal to the
parameter. These collections will generally contain less than 10 items, so I don't intend to use a HashTable.
I really don't know where to start. Dose any one know where I can find a
sample which dose something like this?

Gary


Nov 15 '05 #2
Hi

Thanks. That was just what I was looking for.

I query your point regarding the performance of the Hashtable. Why would MS
then create the System.Collections.Specialized.HybridDictionary ?

Where can I find out how a HashTable works internaly?

Gary
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:eZ*************@TK2MSFTNGP10.phx.gbl...
Hi,

See the documentation and examples on the System.Collections.DictionaryBase class. It uses a HashTable internally, but is very convenient for building
strong-typed dictionaries.

On the other hand, Hashtable can be optimized for different amounts of data stored so there's no reason to worry about its performance.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Gary van der Merwe" <ga*****@hotmail.com> wrote in message
news:eC*************@tk2msftngp13.phx.gbl...
Hi

I want to write my own collection object. I want it to have the following features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The
indexer should return the first item that's ID property is equal to the
parameter. These collections will generally contain less than 10 items,
so I
don't intend to use a HashTable.
I really don't know where to start. Dose any one know where I can find a
sample which dose something like this?

Gary

Nov 15 '05 #3
Hi,
I query your point regarding the performance of the Hashtable. Why would MS then create the System.Collections.Specialized.HybridDictionary ?
According to MSDN, this class automatically accomodates the type of internal
storage to the amount of data stored - and this is transparent to the user,
I assume. But Hashtable itself can also be fine-tuned through specifying the
so called "load factor".

I have actually never tested Hashtable performance for small amounts of
data - so there might really be overhead related to managing lookup trees
etc. As for the internal implementation, I think there should be something
well-known (probably from the Donald Knuth's book on algorithms).

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Gary van der Merwe" <ga*****@hotmail.com> wrote in message
news:OI**************@tk2msftngp13.phx.gbl... Hi

Thanks. That was just what I was looking for.

I query your point regarding the performance of the Hashtable. Why would MS then create the System.Collections.Specialized.HybridDictionary ?

Where can I find out how a HashTable works internaly?

Gary
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:eZ*************@TK2MSFTNGP10.phx.gbl...
Hi,

See the documentation and examples on the System.Collections.DictionaryBase
class. It uses a HashTable internally, but is very convenient for building
strong-typed dictionaries.

On the other hand, Hashtable can be optimized for different amounts of

data
stored so there's no reason to worry about its performance.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Gary van der Merwe" <ga*****@hotmail.com> wrote in message
news:eC*************@tk2msftngp13.phx.gbl...
Hi

I want to write my own collection object. I want it to have the

following features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The indexer should return the first item that's ID property is equal to the parameter. These collections will generally contain less than 10 items, so
I
don't intend to use a HashTable.
I really don't know where to start. Dose any one know where I can find

a sample which dose something like this?

Gary



Nov 15 '05 #4
Gary,
I posted an ExDictionaryBase VB.NET sample class that works mostly like
DictionaryBase except it allows you to use HybridDictionary instead of
HashTable, actually it allows you to use any IDictionary instead of
HashTable. I wrote it for just this reason, where I want a type safe
dictionary but not use HashTable.

See
http://groups.google.com/groups?q=Ex...phx.gbl&rnum=1

However currently I do not see my post with the attachments on google. If
you cannot locate the attachments I can send them to you.

I really need to get them on www.gotdotnet.com

Hope this helps
Jay

"Gary van der Merwe" <ga*****@hotmail.com> wrote in message
news:OI**************@tk2msftngp13.phx.gbl...
Hi

Thanks. That was just what I was looking for.

I query your point regarding the performance of the Hashtable. Why would MS then create the System.Collections.Specialized.HybridDictionary ?

Where can I find out how a HashTable works internaly?

Gary
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:eZ*************@TK2MSFTNGP10.phx.gbl...
Hi,

See the documentation and examples on the System.Collections.DictionaryBase
class. It uses a HashTable internally, but is very convenient for building
strong-typed dictionaries.

On the other hand, Hashtable can be optimized for different amounts of

data
stored so there's no reason to worry about its performance.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Gary van der Merwe" <ga*****@hotmail.com> wrote in message
news:eC*************@tk2msftngp13.phx.gbl...
Hi

I want to write my own collection object. I want it to have the

following features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The indexer should return the first item that's ID property is equal to the parameter. These collections will generally contain less than 10 items, so
I
don't intend to use a HashTable.
I really don't know where to start. Dose any one know where I can find

a sample which dose something like this?

Gary


Nov 15 '05 #5
You can use a ListDictionary object to save resources. Microsoft
recommends this for collections that contain less than 10 items
because it uses a linked list. A simple sample follows..

public class MyCollection
{
private ListDictionary mList = new ListDictionary();
public object this[string Key]
{
set
{
if(!this.mList.Contains(Key))
this.mList.Add(Key, value);
else
mList[VarName] = value;
}
get
{
if(this.mList.Contains(Key))
return this.mList[Key];
else
return null;
}
}
public void Remove(string Key)
{
if(this.mList.Contains(Key))
this.mList.Remove(Key);
else
return;//You may throw an exception here...

}
}

I just implemented indexer and remove because indexers set method
dynamically adds.

"Gary van der Merwe" <ga*****@hotmail.com> wrote in message news:<eC*************@tk2msftngp13.phx.gbl>...
Hi

I want to write my own collection object. I want it to have the following
features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The
indexer should return the first item that's ID property is equal to the
parameter. These collections will generally contain less than 10 items, so I
don't intend to use a HashTable.
I really don't know where to start. Dose any one know where I can find a
sample which dose something like this?

Gary

Nov 15 '05 #6
Zubeyr Dereli <zu****@vom.com.tr> wrote:
You can use a ListDictionary object to save resources. Microsoft
recommends this for collections that contain less than 10 items
because it uses a linked list. A simple sample follows..

public class MyCollection
{
private ListDictionary mList = new ListDictionary();
public object this[string Key]
{
set
{
if(!this.mList.Contains(Key))
this.mList.Add(Key, value);
else
mList[VarName] = value;
}
get
{
if(this.mList.Contains(Key))
return this.mList[Key];
else
return null;
}
}
public void Remove(string Key)
{
if(this.mList.Contains(Key))
this.mList.Remove(Key);
else
return;//You may throw an exception here...

}
}


I'm assuming that the "VarName" bit in the setter is meant to be "Key".

Note that the above can be written as:

public class MyCollection
{
private ListDictionary mList = new ListDictionary();

public object this[string Key]
{
set
{
mList[Key]=value;
}
get
{
return mList[Key];
}
}

public void Remove(string Key)
{
mList.Remove(Key);
}
}

There's no need for all the checking for whether or not the list
contains the key - the indexer of ListDictionary does everything for
you.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
Hi All

Thanks for all your answers and examples. It has been a great help.

For the examples, when adding a item to the collection, one has to pass two
objects, a key and a value. Generally the key is a string, and the value is
of some type. The type of the value objects that I want to add to my
collection has a ID property. I want this property to be the key. How would
I do this.

Gary
Nov 15 '05 #8
Gary,
Have you tried something like:

public class MyItem
{
//property ID
}
public class MyCollection
{
public void Add(MyItem item)
{ this.mList.Add(item.ID, item); }
}
You cannot do it with just the indexer as the indexer expects a key and
returns or accepts an object.

Hope this helps
Jay

"Gary van der Merwe" <ga*****@hotmail.com> wrote in message
news:OE**************@TK2MSFTNGP11.phx.gbl... Hi All

Thanks for all your answers and examples. It has been a great help.

For the examples, when adding a item to the collection, one has to pass two objects, a key and a value. Generally the key is a string, and the value is of some type. The type of the value objects that I want to add to my
collection has a ID property. I want this property to be the key. How would I do this.

Gary

Nov 15 '05 #9
You can find out some of the answers by looking at the source to Rotor.

However, there is a simple explanation.

The HybridDictionary uses a list for less than 10 items. It uses a
Hashtable for more than that.

There is an overhead, both in performance and in memory usage for a
hashtable. If you are using a whole lot of dictionaries, say in a
hierarchy, you may have a few that have lots of items, and many that have
only a few items. By dynamically adjusting the underlying type, you get
better performance and near optimal memory usage in this case.

If you were to use only Hashtable dictionaries, the memory usage would be
much higher, and the performance would be slightly slower.

If you were to use only list-based dictionaries, the memory usage would be
optimal, but the performance on searches on the larger nodes would be
terribly slow.
Again, the Rotor hashtable will shed a lot of light on this if you feel like
digging. Otherwise, just look up hash based searches in any college level
algorithms book.
"Gary van der Merwe" <ga*****@hotmail.com> wrote in message
news:OI**************@tk2msftngp13.phx.gbl...
Hi

Thanks. That was just what I was looking for.

I query your point regarding the performance of the Hashtable. Why would MS then create the System.Collections.Specialized.HybridDictionary ?

Where can I find out how a HashTable works internaly?

Gary
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:eZ*************@TK2MSFTNGP10.phx.gbl...
Hi,

See the documentation and examples on the System.Collections.DictionaryBase
class. It uses a HashTable internally, but is very convenient for building
strong-typed dictionaries.

On the other hand, Hashtable can be optimized for different amounts of

data
stored so there's no reason to worry about its performance.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Gary van der Merwe" <ga*****@hotmail.com> wrote in message
news:eC*************@tk2msftngp13.phx.gbl...
Hi

I want to write my own collection object. I want it to have the

following features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The indexer should return the first item that's ID property is equal to the parameter. These collections will generally contain less than 10 items, so
I
don't intend to use a HashTable.
I really don't know where to start. Dose any one know where I can find

a sample which dose something like this?

Gary


Nov 15 '05 #10

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

Similar topics

20
by: Brian Burgess | last post by:
Hi all, Anyone know if this is possible? If so, on which page would the cookie be? .. On the page calling a function defined in the include file? thanks in advance.. -BB
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
9
by: Kelly Vernon | last post by:
I have a standard ASP page that appends to an xml page. Currently if there is more than one person attempting to append to the same XML file at a time. One user will have the ability to append,...
2
by: bradbretao | last post by:
Hi I'm very new to Visual Basic .Net, I am stuck on a piece of programming and in need of some help! I don't know whether I should be using a 'For... Next Loop' or a 'Do... Loop' (?) The...
385
by: Xah Lee | last post by:
Jargons of Info Tech industry (A Love of Jargons) Xah Lee, 2002 Feb People in the computing field like to spur the use of spurious jargons. The less educated they are, the more they like...
10
by: Aaron | last post by:
Hello, I have a small application that I need to save data from 7 text boxes in to a csv file. This will entail btnNext_Click function that will create a new csv file and enter the 7 data fields...
0
by: alex | last post by:
I am trying to implement "Ban user IP" functionality in my webapp by writing directly to IIS6 metabase. There is IPSecurity section that allows deny access for specific ip addresses. Here Ive found...
6
by: BobAchgill | last post by:
Has anyone had success with writing text using a private font that was not already installed to the system? When I try the example code in NET Framework Developer's Guide called “Creating a...
9
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.