how to over ride operator []?

  • Thread starter Richard Lewis Haggard
  • Start date
R

Richard Lewis Haggard

What is the syntax to over ride the array index operator '[]'?

I want to derive a class from CollectionBase and have it encapsulate an
array of another class I'm working with. Among other things, it needs to
over ride the [] operator but I don't seem to be able to get the syntax
quite right. Can someone tell me what the syntax should be in order to over
ride the operator[]?
 
D

Daniel O'Connell [C# MVP]

Richard Lewis Haggard said:
What is the syntax to over ride the array index operator '[]'?

I want to derive a class from CollectionBase and have it encapsulate an
array of another class I'm working with. Among other things, it needs to
over ride the [] operator but I don't seem to be able to get the syntax
quite right. Can someone tell me what the syntax should be in order to
over ride the operator[]?

You have to define an indexer:

public <type> this[<parameter list>]
{
get { }
set { }
}
 
A

ALI RAZA

Salam

In addition to Richard Lewis Haggard solution, you must also use a OVERRIDE
keyword, in order to override this function.

--
ALI RAZA SHAIKH
MCAD.net

www.programmersparadise.cjb.net
alirazashaikh.blogspot.com

Daniel O'Connell said:
Richard Lewis Haggard said:
What is the syntax to over ride the array index operator '[]'?

I want to derive a class from CollectionBase and have it encapsulate an
array of another class I'm working with. Among other things, it needs to
over ride the [] operator but I don't seem to be able to get the syntax
quite right. Can someone tell me what the syntax should be in order to
over ride the operator[]?

You have to define an indexer:

public <type> this[<parameter list>]
{
get { }
set { }
}
 
D

Daniel O'Connell [C# MVP]

ALI RAZA said:
Salam

In addition to Richard Lewis Haggard solution, you must also use a
OVERRIDE keyword, in order to override this function.

Note that this is only applicable when the indexer is overriding a virtual
indexer on the parent class. It isn't required for new indexers.
 
R

Richard Lewis Haggard

Thanks, but I couldn't quite seem to implement your suggestion. Here's what
I want to do - make a class that's derived from ArrayList and contains an
array of CAccount objects. I tried this:

class CAccounts : ArrayList
{
public override CAccount this[ int iIndex]
{
get { List[iIndex]; }
set { List[iIndex]; }
}
}
But that resulted in the following error:

'HAPTimeClock.CAccounts.this[int]: type must be 'object' to match overriden
member 'System.Collections.ArrayList.this[int]

I tried several more variations but nothing seemed to make the C# compiler
happy.

--
Richard Lewis Haggard

Daniel O'Connell said:
Richard Lewis Haggard said:
What is the syntax to over ride the array index operator '[]'?

I want to derive a class from CollectionBase and have it encapsulate an
array of another class I'm working with. Among other things, it needs to
over ride the [] operator but I don't seem to be able to get the syntax
quite right. Can someone tell me what the syntax should be in order to
over ride the operator[]?

You have to define an indexer:

public <type> this[<parameter list>]
{
get { }
set { }
}
 
D

Daniel O'Connell [C# MVP]

Richard Lewis Haggard said:
Thanks, but I couldn't quite seem to implement your suggestion. Here's
what I want to do - make a class that's derived from ArrayList and
contains an array of CAccount objects. I tried this:

class CAccounts : ArrayList
{
public override CAccount this[ int iIndex]
{
get { List[iIndex]; }
set { List[iIndex]; }
}
}
But that resulted in the following error:

'HAPTimeClock.CAccounts.this[int]: type must be 'object' to match
overriden member 'System.Collections.ArrayList.this[int]

I tried several more variations but nothing seemed to make the C# compiler
happy.

Thats because you cannot overload based on return type. Don't use ArrayList,
try using CollectionBase instead.
--
Richard Lewis Haggard

Daniel O'Connell said:
Richard Lewis Haggard said:
What is the syntax to over ride the array index operator '[]'?

I want to derive a class from CollectionBase and have it encapsulate an
array of another class I'm working with. Among other things, it needs to
over ride the [] operator but I don't seem to be able to get the syntax
quite right. Can someone tell me what the syntax should be in order to
over ride the operator[]?

You have to define an indexer:

public <type> this[<parameter list>]
{
get { }
set { }
}
 

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