static indexer

A

Andrus

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.
 
M

Mattias Sjögren

returns error
modifier static is not allowed for this item.

How to use static indexer ?

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


Mattias
 
J

Jon Skeet [C# MVP]

Peter Bromberg said:
What Mattias is telling you is that an indexer requires "this" - which is an
instance reference. Static = "no instance".

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).)
 
A

Andrus

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).)

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.
 
J

Jon Skeet [C# MVP]

Andrus said:
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).)

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 ?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top