473,324 Members | 2,535 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,324 software developers and data experts.

ArrrayList collection property indexer - please help.

Hi,
Hope I have the right forum here - apologies if I don't. I'm trying to
access a ArrayList collection via an indexer inside a class called
"collExample"
What I want to be able to do is to store and retrieve *any* type of
object to the ArrayList. However I am getting conversion errors - Can
anyone helpe me please?
**** CODE AS FOLLOWS *****
public class collExample
{

private System.Collections.ArrayList AL = new
System.Collections.ArrayList();
public System.Collections.ArrayList this[int idx]
{
get
{
return (System.Collections.ArrayList) AL [idx];
}
set
{
AL [idx] = value;
}
}
}// end collExample

I call it then in the following fashion:

collexample CE = new collExample();

CE[0].Add("Banana");
I'm unsure how to call the object though also. Can anyone help me
please?

Thanks,
Al.

Nov 17 '05 #1
4 2638
I didn's see in the example code where you create the ArrayList AL[0]. IN
this case, it'll be null.
collexample CE = new collExample();
CE[0] = new System.Collections.ArrayList();
CE[0].Add("Banana");

- Shuvro
SDE, MSFT
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

<al*****@altavista.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi,
Hope I have the right forum here - apologies if I don't. I'm trying to
access a ArrayList collection via an indexer inside a class called
"collExample"
What I want to be able to do is to store and retrieve *any* type of
object to the ArrayList. However I am getting conversion errors - Can
anyone helpe me please?
**** CODE AS FOLLOWS *****
public class collExample
{

private System.Collections.ArrayList AL = new
System.Collections.ArrayList();
public System.Collections.ArrayList this[int idx]
{
get
{
return (System.Collections.ArrayList) AL [idx];
}
set
{
AL [idx] = value;
}
}
}// end collExample

I call it then in the following fashion:

collexample CE = new collExample();

CE[0].Add("Banana");
I'm unsure how to call the object though also. Can anyone help me
please?

Thanks,
Al.

Nov 17 '05 #2
Hi Shuvro,

I ran this and not I get an error message just after "AL [idx] =
value;"

The errror message is as follows:

**** Error Message ****

An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative
and less than the size of the collection.
Any ideas? I'm totally confused. Please help!
Al.
Shuvro Mazumder [MSFT] wrote:
I didn's see in the example code where you create the ArrayList AL[0]. IN
this case, it'll be null.
collexample CE = new collExample();
CE[0] = new System.Collections.ArrayList();
CE[0].Add("Banana");

- Shuvro
SDE, MSFT
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

<al*****@altavista.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi,
Hope I have the right forum here - apologies if I don't. I'm trying to
access a ArrayList collection via an indexer inside a class called
"collExample"
What I want to be able to do is to store and retrieve *any* type of
object to the ArrayList. However I am getting conversion errors - Can
anyone helpe me please?
**** CODE AS FOLLOWS *****
public class collExample
{

private System.Collections.ArrayList AL = new
System.Collections.ArrayList();
public System.Collections.ArrayList this[int idx]
{
get
{
return (System.Collections.ArrayList) AL [idx];
}
set
{
AL [idx] = value;
}
}
}// end collExample

I call it then in the following fashion:

collexample CE = new collExample();

CE[0].Add("Banana");
I'm unsure how to call the object though also. Can anyone help me
please?

Thanks,
Al.


Nov 17 '05 #3
<al*****@altavista.com> wrote:
I ran this and not I get an error message just after "AL [idx] =
value;"

The errror message is as follows:

**** Error Message ****

An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative
and less than the size of the collection.

Any ideas? I'm totally confused. Please help!


The problem is that you can't *add* elements to an ArrayList with an
indexer. You'd need to call Add enough times to get the appropriate
number of elements.

Personally I'd not have a "setting" indexer at all - just make the
"getting" one check the current size and call Add (passing in a new
ArrayList each time) until the size is appropriate.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
You're right. My bad. Try the following:

collexample CE = new collExample();
CE.Add(new System.Collections.ArrayList());
CE[0].Add("Banana");
--
- Shuvro
SDE, MSFT

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<al*****@altavista.com> wrote:
I ran this and not I get an error message just after "AL [idx] =
value;"

The errror message is as follows:

**** Error Message ****

An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative
and less than the size of the collection.

Any ideas? I'm totally confused. Please help!


The problem is that you can't *add* elements to an ArrayList with an
indexer. You'd need to call Add enough times to get the appropriate
number of elements.

Personally I'd not have a "setting" indexer at all - just make the
"getting" one check the current size and call Add (passing in a new
ArrayList each time) until the size is appropriate.

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

Nov 17 '05 #5

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

Similar topics

3
by: jamie_m_ | last post by:
I have a custom collection ... clFile that INHERITS from NameObjectCollectionBase the problem is, when I try to create an xmlserializer instance i get an error You must implement a default...
9
by: Gerald Lightsey | last post by:
I am doing some work that involves automating MS MapPoint 2002. The object model is documented in the context of Visual Basic 6.0. A typical example follows. 'Output first result of find...
9
by: Gary van der Merwe | last post by:
Hi I want to write my own collection object. I want it to have the following features. 1.. It must be strongly typed (to a Class that I have written). 2.. I should be able to add and...
4
by: Matt | last post by:
Hi, I've been thinking about how to do this, but can't think of a solution. I have a class that is derived from System.Web.UI.WebControls.DataGrid which works a treat, but I'd like to extend...
3
by: JJ | last post by:
Hi, I have created an Arraylist object from an Arraylist class. I added rows to the arraylist object and I need to find a particular record in my arraylist. How do I do this? Also if I was in...
1
by: almurph | last post by:
Hi, Hope that you can help me please. I'm trying to implement a simple collection property using ArrayList as an indexer. i want this to be pretty generic However I'm having difficulty with...
4
by: Geordie | last post by:
Hi, I'm in the process of converting a production VS 2003 application to VS 2005. To simplify the conversion I'm converting a small piece at a time and then unit testing the code to confirm it...
1
by: SP | last post by:
I have created an abstract class inheriting from KeyedCollection<long, TItemto use as a base class for my collections. In some derived classes I am providing a new indexer property for the key...
5
by: Nikolay Belyh | last post by:
I have created a "collection" in C# like this: namespace ClassLibrary1 { public class X {} public class Class1 { public ArrayList objs {
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.