ArrrayList collection property indexer - please help.

A

almurph

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.
 
S

Shuvro Mazumder [MSFT]

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
 
A

almurph

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.

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.



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.
 
J

Jon Skeet [C# MVP]

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.
 
S

Shuvro Mazumder [MSFT]

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

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