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

Multiple Indexers

Hi Everyone,

I have a class that has two instance variables that are Dictionaries. Now
in addition to having properties to access these dicitionaries, I wanted to
have a properties. But it seems like there can only be one in the class. Am
I missing something?

public class LSItem
{
enum DrawingAttributes { PenWidth, PenColor, FillColor,
TransformA..... };
private Dictionary<DrawingAttributes, floatattributes;
private Dictionary<DrawingAttributes, floatotherattributes;

//I know that I can have an index like so
public float this[DrawingAttributes i]
{
get { return attributes[i]; }
}

//I would have thought that I could do this:
public float DrawingAttribute[DrawingAttributes i]
{
get {...}
set {...}
}

public float OtherAttribute[DrawingAttributes i]
{
get {...}
set {...}
}
}

That way I could have
LSItem item = new LSItem();
float penWidth = item.DrawingAttribute[LSItem.DrawingAttributes.PenWidth];

AliR.
Aug 21 '08 #1
2 1830
On Thu, 21 Aug 2008 09:13:42 -0700, AliR (VC++ MVP) <Al**@online.nospam>
wrote:
Hi Everyone,

I have a class that has two instance variables that are Dictionaries.
Now
in addition to having properties to access these dicitionaries, I wanted
to
have a properties. But it seems like there can only be one in the
class. Am
I missing something?
When you write "I wanted to have a properties", do you actually mean you
want indexers, one for each dictionary?

If so, then the basic answer is no, you can't do that, not in C# (my
recollection is that the CLR supports it and that VB.NET has a syntax that
allows it). The closest you can come is to overload the indexer in your
class, using one for one dictionary and one for the other. But that would
require that each dictionary use an index of different type, which doesn't
appear to be the case in your situation (though, I suppose you could force
it by creating two special indexing types for the purpose).

Alternatively, since you seem to be indexing your dictionaries by a custom
enumeration, presumably where values associated with any given key will
only ever be found in one or the other dictionary, you could have a single
indexer that examines the index first and selects the appropriate
dictionary based on the key (for example, define your keys so that all for
each dictionary fall into a particular range, or have a particular bit set
or something like that).

All that said, I think you should avoid using an indexer in a class unless
that class is really itself semantically a collection. If it's not, then
an explicit method for the purpose would be much better IMHO. For
example, instead of a "DrawingAttribute" and "OtherAttribute" property
that you index, just have a "GetDrawingAttribute()" and
"GetOtherAttribute()" method. This makes it more clear that the "LSItem"
class isn't really just a collection of these attributes, but instead is
an object that _has_ attributes of different classifications.

Pete
Aug 21 '08 #2
Thanks Pete. I appreciate you input.

AliR.
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 21 Aug 2008 09:13:42 -0700, AliR (VC++ MVP) <Al**@online.nospam>
wrote:
>Hi Everyone,

I have a class that has two instance variables that are Dictionaries.
Now
in addition to having properties to access these dicitionaries, I wanted
to
have a properties. But it seems like there can only be one in the
class. Am
I missing something?

When you write "I wanted to have a properties", do you actually mean you
want indexers, one for each dictionary?

If so, then the basic answer is no, you can't do that, not in C# (my
recollection is that the CLR supports it and that VB.NET has a syntax that
allows it). The closest you can come is to overload the indexer in your
class, using one for one dictionary and one for the other. But that would
require that each dictionary use an index of different type, which doesn't
appear to be the case in your situation (though, I suppose you could force
it by creating two special indexing types for the purpose).

Alternatively, since you seem to be indexing your dictionaries by a custom
enumeration, presumably where values associated with any given key will
only ever be found in one or the other dictionary, you could have a single
indexer that examines the index first and selects the appropriate
dictionary based on the key (for example, define your keys so that all for
each dictionary fall into a particular range, or have a particular bit set
or something like that).

All that said, I think you should avoid using an indexer in a class unless
that class is really itself semantically a collection. If it's not, then
an explicit method for the purpose would be much better IMHO. For
example, instead of a "DrawingAttribute" and "OtherAttribute" property
that you index, just have a "GetDrawingAttribute()" and
"GetOtherAttribute()" method. This makes it more clear that the "LSItem"
class isn't really just a collection of these attributes, but instead is
an object that _has_ attributes of different classifications.

Pete

Aug 21 '08 #3

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

Similar topics

12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
3
by: DKode | last post by:
Ok, Consider the following example: School Class - StudentCollection Property StudentCollection Class : CollectionBase - Add - Item
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....
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...
2
by: Raja Raman Sundararajan | last post by:
Hello guys, I was investigating how one can use the "text indexers" in python and I stumbled across several ones. eg., pylucene I wanted to know how the algorithm of indexers look like. I have...
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...
3
by: daz_oldham | last post by:
Hi everyone I am just trying to find how to implement Indexers... or at least that is what I think I want to implement! Very basically, in my database I have a table of T_Hotels which has...
6
by: Beorne | last post by:
I have to use XmlSerializer to serialize a class and I've found big problems serializing properties and indexers. I assumed that if you serialize a class with public properties (or an indexers)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?

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.