Inserting/Adding objects to Collections

  • Thread starter Thread starter Vishal Somaiya
  • Start date Start date
V

Vishal Somaiya

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
 
...
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
 
Thanks Bjorn

Works like a dream!

Vishal


Bjorn Abelli said:
...


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:




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
 
Back
Top