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 / January 2005

Tip: Looking for answers? Try searching our database.

ArrayList question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Scott - 31 Jan 2005 16:39 GMT
Hi,

I have an application where I am attempting to pass an ArrayList of
ArrayList objects from a function to another function.  I have created a
sample application as follows:

using System;
using System.Collections;

namespace ArrayListTest
{
 class Class1
 {
   static void Main(string[] args)
   {
     ArrayList a1, a2;
     a2 = null;

     a1 = GetArrayList();
     Console.WriteLine("Output");
     for (int i = 0; i < a1.Count; i++)
     {
       a2 = (ArrayList)a1[i];
       Console.WriteLine((string)a2[0] + "\t" + (int)a2[1]);
     }
     Console.ReadLine();
   }

   static ArrayList GetArrayList()
   {
     ArrayList result = new ArrayList();
     ArrayList row = new ArrayList();

     for (int i = 0; i < 3; i++)
     {
       row.Add(i.ToString());
       row.Add(i);
       result.Add(row);
     }

     return result;
   }
 }
}
Ideally this should output:

0     0
1     1
2     2

When I trace the code, the function contains an ArrayList of ArrayLists, as
expected, but when it gets passed to the main function, it seems to loose the
contents of the inner array list.  I have also tried using "ref" to pass in
an ArrayList, rather than returning the ArrayList, but it still does not
work.  Does anyone know why the program behaves like this?  Thanks

Scott
NuTcAsE - 31 Jan 2005 17:41 GMT
Add the following code:

for (int i = 0; i < 3; i++)
{
row.Add (i.ToString());
row.Add (i);
result.Add (row);
row = new ArrayList() //Code to be added.
}

In the for loop your adding the same row arraylist reference to the
result arraylist.
Scott - 31 Jan 2005 18:29 GMT
Thanks that fixed the problem.

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.