Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Inserting/Adding objects to Collections

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vishal Somaiya - 11 Aug 2005 17:21 GMT
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
Bjorn Abelli - 11 Aug 2005 17:33 GMT
"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())
[quoted text clipped - 5 lines]
>      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
Vishal Somaiya - 12 Aug 2005 09:09 GMT
Thanks Bjorn

Works like a dream!

Vishal

> "Vishal Somaiya" wrote...
>
[quoted text clipped - 38 lines]
>
> // Bjorn A

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.