473,890 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
"collExampl e"
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.Collecti ons.ArrayList AL = new
System.Collecti ons.ArrayList() ;
public System.Collecti ons.ArrayList this[int idx]
{
get
{
return (System.Collect ions.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 2664
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.Collecti ons.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*****@altavi sta.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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
"collExampl e"
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.Collecti ons.ArrayList AL = new
System.Collecti ons.ArrayList() ;
public System.Collecti ons.ArrayList this[int idx]
{
get
{
return (System.Collect ions.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.Argumen tOutOfRangeExce ption'
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.Collecti ons.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*****@altavi sta.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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
"collExampl e"
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.Collecti ons.ArrayList AL = new
System.Collecti ons.ArrayList() ;
public System.Collecti ons.ArrayList this[int idx]
{
get
{
return (System.Collect ions.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*****@altavi sta.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.Argumen tOutOfRangeExce ption'
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.co m>
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.Collecti ons.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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<al*****@altavi sta.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.Argumen tOutOfRangeExce ption'
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.co m>
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
12869
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 accessor on brWAP.clFile because it inherits from ICollectio I must be having a thick day because I dont have a clue what the error message means. (I have written a defaut property called ITEM in the clFile class 'Classes to abstract a FILE entity...
9
8019
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 search Set objFindResults = objApp.ActiveMap.FindAddressResults("One Microsoft Way", "Redmond", , "WA", , geoCountryUnitedStates) MsgBox "The first item in the find list is: " _ + objFindResults.Item(1).Name
9
1682
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 remove items dynamically. 3.. It should have an indexer. This will have a string parameter. The
4
3436
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 the DataGridColumnCollection class (property of DataGrid) just a fraction by allowing a string indexer. i.e. rather than using myDataGrid.Columns.Visible = false;
3
3762
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 the middle of building the arraylist and wanted to call the last record added to arraylist object how can I do this? Thanks,
1
1079
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 the conversions. I know that System.Collection.ArrayList accepts and returns most of it's members as objects but it's giving an error message of:
4
1684
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 converted correctly. The application consists of 10 seperate projects. I have hit a problem with converting the core object model project. The object model consists of hierarchical data some of which implement the IBinding and IBindingList...
1
3038
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 (long). I need to access my collection by index and by key however the new long indexer is used for BOTH int and long, i.e. myCollection will use the long indexer NOT the base classes int indexer. Why is that? The workaround is to also define a new...
5
3610
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
9976
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9616
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8007
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5834
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4662
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4258
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.