473,890 Members | 5,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between indexers / collections?

Ok,

Consider the following example:

School Class
- StudentCollecti on Property

StudentCollecti on 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 2316
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();
SchoolCollectio n schoolColl = new SchoolCollectio n();

schoolColl[mySchool.Name] = mySchool;

Am i headed in the right direction here?

"Eric Cadwell" <ec******@ns.in sight.com> wrote in message news:<Om******* *******@TK2MSFT NGP09.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
1634
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
3618
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 cannot in other? And finally why C# seems to be a sort of standard in industry? Regards, Raj Dhrolia.
12
24842
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
2
1992
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. What are those strings? </jim>
1
7580
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. There seems to be other ways of doing the jobs of the two and less confusing. The books I read don't provide good examples of situations when I would need an array or indexer. I don't really need a definition of them as I already have that. I...
5
1939
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 that something in a IL Assembly is supposed to be an indexer ? And more important, what ways do I have to influence that ?
4
9876
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 based on their data and some other data as well. i planned on passing it by reference into a function that modified their values. but it gave me the error "you cannot pass indexers and properties by reference". The only way i know of doing it is...
1
1477
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 Studio\PROJECTS\VisualSystems.DbObjects.DbClasses\DbObjectTable.cs 51 30 VisualSystems.DbObjects.DbClasses on this line: string primaryKeyName = "ID";
3
1677
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 function? thanks for all
0
9979
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
10827
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9637
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8017
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
7170
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5854
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...
1
4681
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
4275
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3281
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.