473,804 Members | 3,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1978
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.3869 9$XM6.28383@att bi_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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
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
3300
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
2614
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 Public part1 as Integer Public part2 as Integer End Structure
5
2437
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 entry and you select another index it goes boom!!!. Funny thing is the SelectedIndex Changed routine fires off perfectly and then crash after the return from this code. Now looking around the news groups it appears to be a bug in the .net...
1
2068
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 only have three weeks to finish it all! My GUI and Web services were finished, but I hadn't tested them. So I linked the GUI to the Web service and started testing them.
5
19604
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 having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
2
1779
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 item in the list is displayed for each item, in other words, item #4 is displayed 4 times. My code is below. Thanks
1
2881
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 returning NULL THUS I m getting exception "Object Refrence not set to an Instance to Object", Here is the code I wrote (ASP.NET 1.1)...
0
1421
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 doesn't help. I still get:
5
14090
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 right way. Thanks!
0
9706
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
10337
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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...
1
7622
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
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
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...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2995
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.