PC Review


Reply
Thread Tools Rate Thread

ArrayList question

 
 
=?Utf-8?B?U2NvdHQ=?=
Guest
Posts: n/a
 
      31st Jan 2005
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

 
Reply With Quote
 
 
 
 
NuTcAsE
Guest
Posts: n/a
 
      31st Jan 2005
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.

 
Reply With Quote
 
=?Utf-8?B?U2NvdHQ=?=
Guest
Posts: n/a
 
      31st Jan 2005
Thanks that fixed the problem.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Arraylist Contains() Question ajay.kalyan@gmail.com Microsoft ASP .NET 0 23rd Aug 2006 04:23 PM
arrayList question NuCoder Microsoft C# .NET 4 16th Jun 2005 05:55 PM
Arraylist Question Roshawn Dawson Microsoft VB .NET 1 21st Feb 2005 06:09 AM
Arraylist simple question (newbie) OT (for a CF project, but not necessarily a CF question?) Keith R Microsoft Dot NET Compact Framework 1 2nd Dec 2003 10:27 PM
ArrayList Question george r smith Microsoft C# .NET 3 14th Nov 2003 11:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:42 PM.