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

Inserting/Adding objects to Collections

Hello

I am trying read from a xml file, pull the values in a object and then add
the object to an ArrayList.
I am using a 'while' loop to move through each node in the xml file and
pulling the required values as needed and setting them to the relevant
property within my object. The reading of the xml file works as it should.

I get a problem when I try to add the object that I created to the
arraylist. It seems to be adding each one, but when I view the results of
the list, it shows the last object passed in for each object stored within
the Arraylist.

Here is the code that is giving me issues:

public ArrayList readXMLClient(string path, string xPathEx)
{

XmlDocument document = new XmlDocument();
document.Load(path);
XPathNavigator navigator = document.CreateNavigator();
int i = document.DocumentElement.ChildNodes.Count;
int j = 0;
ClientData objClientData = new ClientData();
ArrayList objArrayList = new ArrayList();

try
{

XPathNodeIterator _clientCode = navigator.Select(xPathEx +
"/ClientCode");
XPathNodeIterator _clientName = navigator.Select(xPathEx +
"/ClientName");
XPathNodeIterator _clientDetails = navigator.Select( xPathEx +
"/ClientDetails");
while (_clientCode.MoveNext() && _clientName.MoveNext() &&
_clientDetails.MoveNext())

{
objClientData.clientCode = _clientCode.Current.Value.ToString();
objClientData.clientName = _clientName.Current.Value.ToString();
objClientData.clientDetails = _clientDetails.Current.Value.ToString();
objArrayList.Insert(j,objClientData);

j++;
}

}

catch(Exception ex)
{

}

return objArrayList;
}

Any help would be great.

Thanking you in advance

Vish
Nov 12 '05 #1
2 2292

"Vishal Somaiya" wrote...
I get a problem when I try to add the object that I created
to the arraylist. It seems to be adding each one, but when
I view the results of the list, it shows the last object
passed in for each object stored within the Arraylist.
You're not adding a *new* object for each iteration, but the same object
every time. Inside the iteration, you change the values of *that* object.

I trim down you're code, so you might see it more clearly:

ClientData objClientData = new ClientData(); // <--
ArrayList objArrayList = new ArrayList();
while (_clientCode.MoveNext() && _clientName.MoveNext()
&& _clientDetails.MoveNext())
{
objClientData.clientCode = _clientCode.Current.Value.ToString();
objClientData.clientName = _clientName.Current.Value.ToString();
objClientData.clientDetails = _clientDetails.Current.Value.ToString();
objArrayList.Insert(j,objClientData);
}

What you can do is simply to move the instantiation inside the iteration:
ArrayList objArrayList = new ArrayList();

while (_clientCode.MoveNext() && _clientName.MoveNext()
&& _clientDetails.MoveNext())
{
ClientData objClientData = new ClientData(); // <--

objClientData.clientCode = _clientCode.Current.Value.ToString();
objClientData.clientName = _clientName.Current.Value.ToString();
objClientData.clientDetails = _clientDetails.Current.Value.ToString();

objArrayList.Insert(j,objClientData);
}
// Bjorn A
Nov 12 '05 #2
Thanks Bjorn

Works like a dream!

Vishal
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"Vishal Somaiya" wrote...
I get a problem when I try to add the object that I created
to the arraylist. It seems to be adding each one, but when
I view the results of the list, it shows the last object
passed in for each object stored within the Arraylist.
You're not adding a *new* object for each iteration, but the same object
every time. Inside the iteration, you change the values of *that* object.

I trim down you're code, so you might see it more clearly:

ClientData objClientData = new ClientData(); // <--
ArrayList objArrayList = new ArrayList();


while (_clientCode.MoveNext() && _clientName.MoveNext()
&& _clientDetails.MoveNext())
{
objClientData.clientCode = _clientCode.Current.Value.ToString();
objClientData.clientName = _clientName.Current.Value.ToString();
objClientData.clientDetails = _clientDetails.Current.Value.ToString();

objArrayList.Insert(j,objClientData);
}

What you can do is simply to move the instantiation inside the iteration:
ArrayList objArrayList = new ArrayList();

while (_clientCode.MoveNext() && _clientName.MoveNext()
&& _clientDetails.MoveNext())
{
ClientData objClientData = new ClientData(); // <--

objClientData.clientCode = _clientCode.Current.Value.ToString();
objClientData.clientName = _clientName.Current.Value.ToString();
objClientData.clientDetails =

_clientDetails.Current.Value.ToString();
objArrayList.Insert(j,objClientData);
}
// Bjorn A

Nov 12 '05 #3

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

Similar topics

11
by: thechaosengine | last post by:
Hi all, I have a very general but quite significant question about objects. My question is, when should I create them? I know thats a crap question so let me explain a bit further. Lets...
2
by: Vishal Somaiya | last post by:
Hello I am trying read from a xml file, pull the values in a object and then add the object to an ArrayList. I am using a 'while' loop to move through each node in the xml file and pulling the...
3
by: Guy Dillen | last post by:
Instead of using DataSets i want to implement an objects collection (in C#). E.g. a class Person and the instances are stored in a collection of Persons. So there is a layer that does the database...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
10
by: Ryan Graham | last post by:
I totally bombed this question in an interview so I'm posting my answer here for comments and suggestions... perhaps (god help me) I'm just not that bright, but this works and seems to be fairly...
3
by: Rob Thomas | last post by:
Hi, I've been tasked to come up with a new architecture for a large application at one of my customer's sites. In the past, I have developed multi-tier applications whereby the business...
1
by: Brian Conklin | last post by:
Hello Eneryone, I am having a problem. I have written a little app that will take a text "pipe" delimited file and place all of the values in to an Excel spreadsheet. It works great on any of my...
8
by: Stefan Mueller | last post by:
I'm really very confused. With the following code I can add rows/fields in frame 1 and 2. If I use IE, Mozilla or Opera the new rows/fields get added in ascending order. However, if I use Safari...
12
by: Bob Jones | last post by:
I have an odd business requirement and I think that the implementation is not correct in the terms of OOP development. Any help on the concepts would be very appreciated! We currently have a...
1
by: =?Utf-8?B?a2thcnJl?= | last post by:
Hi all, We decided to clear the test db more frequently and I was asked to serialize som test objects to make sure our unit testing continues to work after clearing the db. Does anyone have...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...

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.