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

how to over ride operator []?

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[]?
--
Richard Lewis Haggard
Nov 17 '05 #1
5 9928

"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
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 { }
} --
Richard Lewis Haggard

Nov 17 '05 #2
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 [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:OQ**************@TK2MSFTNGP12.phx.gbl...

"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
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 { }
}
--
Richard Lewis Haggard


Nov 17 '05 #3

"ALI RAZA" <al*************@yahoo.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
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.
Nov 17 '05 #4
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 [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:OQ**************@TK2MSFTNGP12.phx.gbl...

"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
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 { }
}
--
Richard Lewis Haggard


Nov 17 '05 #5

"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
news:ur**************@tk2msftngp13.phx.gbl...
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 [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:OQ**************@TK2MSFTNGP12.phx.gbl...

"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
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 { }
}
--
Richard Lewis Haggard



Nov 17 '05 #6

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

Similar topics

14
by: Reply Via News Group Please | last post by:
Folks, I'm new to CSS and realise that by posting in the html newsgroup, I might upset one or two readers by raising a CSS question in an html newsgroup however my ISP only has one small CSS...
6
by: Udaya Bhaskar | last post by:
To differentiate between postfix and prefix opeators , when overloaded, we need to pass a dummy parameter. Why should we pass ONLY int to overload postfix ++ / -- operator? Why not a FLAOT/...
4
by: nicolas | last post by:
Hello,everybody I get a problem: 1. Have an Int Array; 2. Run iterate over a range of Array once; 3. Get Minimum result and No.2 minimum result;
16
by: Alfred Taylor | last post by:
I've been flipping through various coding standards documents for C#/.NET and all of them say to use: if ( boolVar ) {...} instead of if ( boolVar == true ) {...} For the life of my I can't...
9
by: Fred Zwarts | last post by:
What is the recommended way to loop over all enum values of a certain enum type? Consider the following definition: typdef enum {A=2, B, C=5, D} E; then for (E x = A; x <= D; ++x) { ... }
10
by: canam | last post by:
Hi: I hope someone can help me. Is there a way to add a password to a particular field in the event that the validation rule must be over written? I've created an input form in a...
2
by: Excel 009 | last post by:
Hi All, I have the appended code which I used for a banner rotator. It works great except that there 1. there is a border which I do not want and 2. on top of the banner there is a black bar...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.