all groups > c# > november 2003 >
You're in the

c#

group:

more then one indexer per class


Re: more then one indexer per class Daniel O'Connell
11/30/2003 4:06:19 PM
c#:

[quoted text, click to view]
Like methods, indexers can be overloaded by parameter only. You can do

public object this[int index];
public object this[string name];
etc, but you cannot create two indexers with the same parameter list.
[quoted text, click to view]

Re: more then one indexer per class Daniel O'Connell
11/30/2003 5:42:46 PM

[quoted text, click to view]
Well, in the simplist case, you are using ArrayList, correct? ArrayList has
an indexer defined, so why not just a property:
class MyClass
{
ArrayList myList;
public IList MyList
{
get
{
return myList;
}
}
}

MyClass myClass = new MyClass();
myClass.MyList[5]; //returns the item at index 5 in the arraylist backed by
myList.
[quoted text, click to view]

Re: more then one indexer per class Michael Giagnocavo [MVP]
11/30/2003 5:48:43 PM
You could use a property (it'll use a method internally), so the syntax
would be myObject.ListX[index].

-mike
MVP

[quoted text, click to view]

Re: more then one indexer per class Daniel O'Connell
11/30/2003 6:27:13 PM
Of course, you can write an indexer that does this as well. Its all a matter
of wht you want to do.
[quoted text, click to view]

Re: more then one indexer per class Frank Oquendo
11/30/2003 7:13:17 PM
[quoted text, click to view]

That's not true. Even so, GetSomething(0) is a whole lot clearer than
object[0, 0].

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)

Re: more then one indexer per class Jay B. Harlow [MVP - Outlook]
11/30/2003 7:22:59 PM
Yoramo,
VB.NET allows properties with parameters, which gets you around the only one
indexer per class.

One "work-around" that I use in C# is what I believe Michael Giagnocavo is
suggesting, or at least a variation of what Michael is suggesting. Define a
read-only property on your class that returns a proxy object that has an
indexer defined. Then each arraylist would have its own property that
returns this proxy object, the proxy object would contain a private member
that refers to the actual arraylist, and a indexer that returns a value from
this arraylist...

Hope this helps
Jay

[quoted text, click to view]

more then one indexer per class Yoramo
11/30/2003 11:52:24 PM
Hello

I have a class the containes several ArrayList's and I whold like to use a
indexer for each one. I'm getting a compilation error.

"already defines a member called 'this' with the same parameter types"


can any one advice:
can I declear more then one indexer?

thanks
Yoramo


Re: more then one indexer per class Alvin Bruney
11/30/2003 11:55:56 PM
Tough. Use a method.


[quoted text, click to view]

Re: more then one indexer per class Alvin Bruney
11/30/2003 11:56:51 PM
You can wait until Fantasy 1.0 .NET or you can have a working solution today
with a method.



[quoted text, click to view]

Re: more then one indexer per class Yoramo
12/1/2003 12:20:53 AM
Thanks

what is the right approach to expose arrays from one object?
I do not like the GetXXX(int indx) type of methods. do you have a better
solution?

Yoramo

[quoted text, click to view]

Re: more then one indexer per class Michael Culley
12/1/2003 11:08:58 AM
[quoted text, click to view]

What is the problem with this?

--
Michael Culley

Re: more then one indexer per class Michael Culley
12/1/2003 11:10:04 AM
You could have a function with 2 params, the second param could be an enum if you like.

GetValue(int Index, int ArrayIndex)

--
Michael Culley


[quoted text, click to view]

Re: more then one indexer per class Jay B. Harlow [MVP - Outlook]
12/1/2003 11:19:07 AM
Jon,
I believe Michael is referring to the fact that VB.NET supports properties
that accept parameters, effectively defining named indexers.

In VB.NET one can do:

Public Class Person

Private m_name As HashTable

Public Property Name(index As String) As String
Get
Return m_name(index)
End Get
Set(ByVal value As String)
m_name (index) = value
End Set
End Property

Public Property PhoneNumber(index As Integer) As String
... ' similar to the Name property

Public Property Address(index As Integer) As String
... ' similar to the Name property

' This property is the same as C#'s indexer
' you can use either obj.Item(0) or obj(0) to get the value
Default Public Property Item(index As Integer) As String
...

End Class

Where the "index" parameter can be any type, and you can define more then
one "index" parameter (multi-dimension constructs).

Then I can use it as:
Dim aPerson As Person

aPerson.Name("First") = "Jay"
aPerson.Name("Middle") = "B"
aPerson.Name("Last") = "Harlow"
aPerson.PhoneNumber(0) = "1234"
aPerson.PhoneNumber(1) = "4321"
aPerson.Name(0) = "My home address"
aPerson.Name(1) = "My street address"
aPerson(0) = "item 1"
aPerson(1) = "item 2"

Which is what the OP wants, that C# does not support, however VB.NET does
support.

Hope this helps
Jay

[quoted text, click to view]

Re: more then one indexer per class Michael Culley
12/1/2003 11:57:14 AM
[quoted text, click to view]

No you can't, an indexer can have only one parameter.

--
Michael Culley

Re: more then one indexer per class Michael Culley
12/1/2003 1:37:20 PM
You are correct, I must have been confusing it with properties where vb.net can have any number of params and C# can only have none.
All this time I've been using indexers with only one param :(

--
Michael Culley


[quoted text, click to view]

Re: more then one indexer per class Jon Skeet [C# MVP]
12/1/2003 4:38:08 PM
[quoted text, click to view]

What exactly do you mean by this? Properties don't have *any*
parameters, unless you're talking about the value of the setter - in
which case, what does VB.NET do if you tell it to set a single property
to multiple values?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: more then one indexer per class Jon Skeet [C# MVP]
12/1/2003 6:00:37 PM
[quoted text, click to view]

Ah yes, calling them "named indexers" makes sense. Calling them
properties that take parameters doesn't make much sense to me, but
there we go - I see that it's what VB.NET calls them.

[quoted text, click to view]

<snip>

[quoted text, click to view]

Yup. I've sometimes found that a pain too - for instance giving read-
only access to a collection or array.

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