more then one indexer per class

  • Thread starter Thread starter Yoramo
  • Start date Start date
Y

Yoramo

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
 
Yoramo said:
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?
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.
 
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

Daniel O'Connell said:
Yoramo said:
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?
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.
thanks
Yoramo
 
Tough. Use a method.


Yoramo said:
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

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?
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.
thanks
Yoramo
 
You can wait until Fantasy 1.0 .NET or you can have a working solution today
with a method.



Yoramo said:
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

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?
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.
thanks
Yoramo
 
Yoramo said:
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?
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.
Yoramo

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?
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.
thanks
Yoramo
 
You could use a property (it'll use a method internally), so the syntax
would be myObject.ListX[index].

-mike
MVP

Yoramo said:
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

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?
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.
thanks
Yoramo
 
Yoramo said:
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?

What is the problem with this?
 
You could have a function with 2 params, the second param could be an enum if you like.

GetValue(int Index, int ArrayIndex)
 
Of course, you can write an indexer that does this as well. Its all a matter
of wht you want to do.
 
Daniel O'Connell said:
Of course, you can write an indexer that does this as well. Its all a matter
of wht you want to do.

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

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

Yoramo said:
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

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?
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.
thanks
Yoramo
 
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


Frank Oquendo said:
Michael said:
No you can't, an indexer can have only one parameter.

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

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,
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
 
Jay B. Harlow said:
I believe Michael is referring to the fact that VB.NET supports properties
that accept parameters, effectively defining named indexers.

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.
In VB.NET one can do:

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

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