473,395 Members | 2,010 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,395 software developers and data experts.

Difference between indexers / collections?

Ok,

Consider the following example:

School Class
- StudentCollection Property

StudentCollection Class : CollectionBase
- Add
- Item
- Remove

Student Class
- Name Property
- Age Property

Now,

Up until this point, whenever I had a set of objects I needed to store
in a collection, I would make a class to handle all of the instances
of the objects (Student Class in this example), and then I would
return the collection to the Presentation layer to perform databinding
to controls. Also in the example above, whenever I would have a 0..*
class relationship I would create a private variable on the 0 side of
the relationship (School Class in this example). To hold my
collections, then I would expose this collection through a property in
School class so I could get only the students for that school back in
a nice neat collection.

Do indexers simplify this process for me or am I thinking about
indexers in the wrong way? The way I see it, indexers are a better way
of handling collections because you can refer to them on a key-value
basis instead of just a numerical index (like in collectionbase)

I guess i'm a little confused in the usability of indexers here. Could
someone clarify?

Thank you.
Nov 15 '05 #1
3 2292
You can use CollectionBase on a Key, Value basis. Just add an overloaded
indexer that accepts a string instead of a int, but you have to be sure that
your key is unique!

public Student this[string name]
{
//iterate the List to find the right one or create a hash map when items
are added.
}
HTH;
Eric Cadwell
http://www.origincontrols.com
Nov 15 '05 #2
Eric,

I'm a little confused,

the peice of code you replied with there, would I add that overloaded
indexer to my class that derives from CollectionBase? and then I could
add/retrieve school instances like so:

School mySchool = new School();
SchoolCollection schoolColl = new SchoolCollection();

schoolColl[mySchool.Name] = mySchool;

Am i headed in the right direction here?

"Eric Cadwell" <ec******@ns.insight.com> wrote in message news:<Om**************@TK2MSFTNGP09.phx.gbl>...
You can use CollectionBase on a Key, Value basis. Just add an overloaded
indexer that accepts a string instead of a int, but you have to be sure that
your key is unique!

public Student this[string name]
{
//iterate the List to find the right one or create a hash map when items
are added.
}
HTH;
Eric Cadwell
http://www.origincontrols.com

Nov 15 '05 #3
Yes, that encapsulates the looping or indexing logic in the collection class
and makes the rest of your code cleaner.

-Eric
Nov 15 '05 #4

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

Similar topics

10
by: Alvin Bruney | last post by:
In later versions of the framework is it possible to have indexers on properties for member fields? Say for example I have private ArrayList blah; and a propget public object Blah
11
by: Raj Dhrolia | last post by:
Hi Guys, It might seem to be a very easy question, but i am very much eager to know some good technical difference between C# and VB.NET. Are there anything that i can do in one language and...
12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
2
by: Jim | last post by:
How does one determine what indexers are available on a given object? The only way I have found is by looking at the Object Browser. But even then it only gives a simple signature like, this....
1
by: mdub317 | last post by:
I'm totally new to programming and I am wondering; when would be a good time to use an array or an indexer? I want to know what types of applications would make good use of arrays or indexers. ...
5
by: bonk | last post by:
Hello, IL does not have indexers. Infact the c# compiler compiles indexers to Set_Item and Get_Item (or whatever name I choose via the IndexerNameAttribute ). So how does c# (compiler) know...
4
by: tg.foobar | last post by:
i'd like to do the following, but i don't think it's possible. can you help me find a way to do this, or maybe a better way to write the code? I have a list of items that need to be modified...
1
by: Andrzej Kaczmarczyk | last post by:
Hi, I am receiving this exception Error 3 Cannot apply indexing with to an expression of type 'System.Collections.Generic.IEnumerable<VisualSystems.DbObjects.DbClasses.DbColumn>' C:\Visual...
3
by: Benssol | last post by:
Hi all great programmers and great coders Please can anyone explain clearly the following: usage of indexers? is it used widely (in most applications)? is there is another way that do its...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.