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

Always returning the last item in the ArrayList

Hi all,

I have an arraylist filled with 4 objects and defined as shown below.

ArrayList items = new ArrayList();

public Item GetItemAt(int index)
{
return (Item)items[index];
}

Problem is, no matter what value I provide for "index", it always returns
the LAST item in the ArrayList.
I feed it index 0, 1, 2, or 3 and it always returns Item at index 3.

I'm confused. Any tips would be greatly appreciated.

Thanks!


Nov 16 '05 #1
3 1946
Works fine for me...
Try to make your own array derived from ArrayList and override This[int
Index] method. Update us if it helps

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Adam" <no****@comcast.net> wrote in message
news:pnOGc.38699$XM6.28383@attbi_s53...
Hi all,

I have an arraylist filled with 4 objects and defined as shown below.

ArrayList items = new ArrayList();

public Item GetItemAt(int index)
{
return (Item)items[index];
}

Problem is, no matter what value I provide for "index", it always returns
the LAST item in the ArrayList.
I feed it index 0, 1, 2, or 3 and it always returns Item at index 3.

I'm confused. Any tips would be greatly appreciated.

Thanks!

Nov 16 '05 #2
Adam <no****@comcast.net> wrote:
I have an arraylist filled with 4 objects and defined as shown below.

ArrayList items = new ArrayList();

public Item GetItemAt(int index)
{
return (Item)items[index];
}

Problem is, no matter what value I provide for "index", it always returns
the LAST item in the ArrayList.
I feed it index 0, 1, 2, or 3 and it always returns Item at index 3.

I'm confused. Any tips would be greatly appreciated.


That suggests you've actually added 4 references, all to the same
object. Could you post the code you're using to populate the list?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
That's exactly what I did wrong, I posted 4 references to the same object.

Thanks for the help!

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Adam <no****@comcast.net> wrote:
I have an arraylist filled with 4 objects and defined as shown below.

ArrayList items = new ArrayList();

public Item GetItemAt(int index)
{
return (Item)items[index];
}

Problem is, no matter what value I provide for "index", it always returns the LAST item in the ArrayList.
I feed it index 0, 1, 2, or 3 and it always returns Item at index 3.

I'm confused. Any tips would be greatly appreciated.


That suggests you've actually added 4 references, all to the same
object. Could you post the code you're using to populate the list?

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

Nov 16 '05 #4

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

Similar topics

3
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
4
by: Tad Marshall | last post by:
Hi, I'm reading about arrays in VB.NET and I seem to have a few options for my data structure. I need a multi-dimensional array of structures, and my first thought was Public Structure myStr...
5
by: John Veldthuis | last post by:
My code works perfectly 100% when adding items to my ArrayList and updating the listbox. Works perfectly when deleting an item in the ArrayList when it is not the last entry but if it is the last...
1
by: Matthias De Ridder | last post by:
Hello, I really hope that someone will be able to help me, because I'm desperate now! I'm a student, graduating this year, and I'm working on a thesis where C# Web Services are involved. I...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
2
by: David Lozzi | last post by:
Hello, I have an arraylist that loads a checkboxlist. I populate the arraylist using a custom class and pulling the data from the web.config file. When the checkboxlist is loaded, only the last...
1
by: Anup | last post by:
Hi Group, I have a datagrid with dropdown in my application. I want to fill the data in dropdown by an ArrayList for that I am using "e.Item.FindControl("DropdownId")" but this function is...
0
by: Igor | last post by:
I have a web method that returns an ArrayList. Everything works fine except that I can't get rid of thos ArrayOfAnyType tags! I've tried using: public ArrayList Method(){...} but it...
5
by: Curious | last post by:
I have an ArrayList outputList. I only need to get the last item: int last = outputList.LastIndexOf(null); ExtremeBucket lastItem = (ExtremeBucket)outputList; Please confirm if this is the...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.