Groups | Blog | Home
all groups > c# > june 2007 >

c# : static indexer


Peter Bromberg [C# MVP]
6/30/2007 2:34:01 PM
What Mattias is telling you is that an indexer requires "this" - which is an
instance reference. Static = "no instance".
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com



[quoted text, click to view]
Andrus
6/30/2007 10:17:54 PM
class myclass {

public static object this[string index] {
get {
return "Row[index]";
}
}
}

returns error

modifier static is not allowed for this item.

How to use static indexer ?

Andrus.

Mattias Sjögren
6/30/2007 10:39:08 PM
[quoted text, click to view]

You can't, that's what the error message is telling you.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Jon Skeet [C# MVP]
6/30/2007 11:20:21 PM
[quoted text, click to view]

Of course, that's just a matter of syntax - supporting static indexers
is perfectly feasible technically, it just isn't supported in C#.

(In some cases it would make a lot of sense, eg: Encoding[encodingName]
instead of Encoding.GetEncoding(encodingName).)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Andrus
7/1/2007 12:00:00 AM
[quoted text, click to view]

I use static class like Invoice in RDL report expressions.

Currently I need to write in all report expressions

Invoice.Get("amount", Fields!InvoiceId.Value)

This makes expressions complicated.
I want to use indexer to simplify this like

Invoice["amount", Fields!InvoiceId.Value]

How to fix this C# bug ?

Andrus.

Jon Skeet [C# MVP]
7/1/2007 7:58:52 PM
[quoted text, click to view]

It's not a bug, it's a design decision with pros and cons. We may both
agree that it would be a nice feature to have, but that doesn't make
the current state "buggy".

However, the way to change things is to post a feature request on
http://connect.microsoft.com

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
AddThis Social Bookmark Button