473,387 Members | 3,821 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,387 software developers and data experts.

more then one indexer per class

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

Nov 15 '05 #1
16 1960

"Yoramo" <yo****@hotmail.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #2
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" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...

"Yoramo" <yo****@hotmail.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
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


Nov 15 '05 #3
Tough. Use a method.
"Yoramo" <yo****@hotmail.com> wrote in message
news:#K**************@TK2MSFTNGP12.phx.gbl...
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" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...

"Yoramo" <yo****@hotmail.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
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



Nov 15 '05 #4
You can wait until Fantasy 1.0 .NET or you can have a working solution today
with a method.

"Yoramo" <yo****@hotmail.com> wrote in message
news:#K**************@TK2MSFTNGP12.phx.gbl...
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" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...

"Yoramo" <yo****@hotmail.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
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



Nov 15 '05 #5

"Yoramo" <yo****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...

"Yoramo" <yo****@hotmail.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
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



Nov 15 '05 #6
You could use a property (it'll use a method internally), so the syntax
would be myObject.ListX[index].

-mike
MVP

"Yoramo" <yo****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...

"Yoramo" <yo****@hotmail.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
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



Nov 15 '05 #7
"Yoramo" <yo****@hotmail.com> wrote in message news:#K**************@TK2MSFTNGP12.phx.gbl...
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?

--
Michael Culley
Nov 15 '05 #8
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
"Yoramo" <yo****@hotmail.com> wrote in message news:eT**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #9
Of course, you can write an indexer that does this as well. Its all a matter
of wht you want to do.
"Michael Culley" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:u7*************@tk2msftngp13.phx.gbl...
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
"Yoramo" <yo****@hotmail.com> wrote in message

news:eT**************@TK2MSFTNGP10.phx.gbl...
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


Nov 15 '05 #10
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message news:#o*************@TK2MSFTNGP11.phx.gbl...
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 Culley
Nov 15 '05 #11
Michael Culley wrote:
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)
Nov 15 '05 #12
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" <yo****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...

"Yoramo" <yo****@hotmail.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
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



Nov 15 '05 #13
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" <fr*******@acadx.com> wrote in message news:#p**************@TK2MSFTNGP12.phx.gbl...
Michael Culley wrote:
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)

Nov 15 '05 #14
Michael Culley <mc*****@NOSPAMoptushome.com.au> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #15
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Michael Culley <mc*****@NOSPAMoptushome.com.au> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #16
Jay B. Harlow [MVP - Outlook] <Ja************@msn.com> wrote:
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:
<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #17

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

Similar topics

14
by: Nikhil Patel | last post by:
Hi all, Is it possible to have more than one indexer in a class. If not, are there any good alternative ways to achieve similar functionality. Thanks... -Nikhil
2
by: Jerry Negrelli | last post by:
I'm trying to define a custom class indexer to take the place of an underlying Hashtable member so that values can be referred to as Class1 instead of Class1.someVar. The following code does...
7
by: Oren | last post by:
hi I have created a class with a simple indexer what is the syntax for adding the function "Remove" ... myClass.colors=Color.Black; // WORKS O myClass.colors.Remove(); // ?? .......
7
by: Steph | last post by:
I'm learning C#. I need to implement an indexer using an array list. I have the following code and I'm getting an error "Inconsistent accessibility: indexer return type CRegs is less accessible...
5
by: Clive Dixon | last post by:
Is it possible to access an indexer of a base class with identical signature, e.g. class Class1 { public object this { get { // ...
8
by: Rainer Queck | last post by:
Hi NG, how can I implement more then one array property to a class? I have read about the Indexer, but as far as I can see this would only work for a Collection type of class like list or...
3
by: Nash Alexx | last post by:
Hello! I am quite new to C#, and one concept that really gives me a headache is "indexer". I have gone through the MSDN examples, and, at some level know how to use indexers. But, the thing is I...
17
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
3
by: Duggi | last post by:
Hello, I have a class with an indexer in the class. The code code is in C#. However I was trying to access the indexer in the other lang of .Net, VB. I ran into some indexer related issues (I...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.