2 questions about array lists

A

Adam Sandler

Hello,

I have 2 questions about using array lists.

1. I have a method which returns an arraylist. I'd like to put the
contents of what is returned into another arraylist:

ArrayList al = new ArrayList();
al.AddRange(obj.getArrayList());

The two lines above fail miserably.

2. Can the contents of an arraylist of strings populate a richtextbox
without iterating through the collection? I've seen this: rtb.Text
+= myList.toString() but is there a built-in method which would do
the same thing?

Thanks,

--Chris
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello,

I have 2 questions about using array lists.

1. I have a method which returns an arraylist.  I'd like to put the
contents of what is returned into another arraylist:

Why?
You mean, you want to copy it?

Have you tried ArrayList.Clone?

Also AddRange should work:
From MSDN:
ArrayList.AddRange Method:
public virtual void AddRange (
ICollection c
)

public class ArrayList : IList, ICollection, IEnumerable,
ICloneable

So AddRange expect an ICollection and ArrayList implements
ICollection, so it should work
 
A

Adam Sandler

So AddRange expect an ICollection and ArrayList implements
ICollection, so it should work

It doesn't. I've typed the code exactly as it appears in the IDE. I
'm aware that arraylists implements icollection; that is why I used
used AddRange.
 
J

JS

ArrayList al = new ArrayList();
al.AddRange(obj.getArrayList());

The two lines above fail miserably.
What does 'fail miserably' mean? Failed to compile/throws an
exception/unexpected behavior? If it is an exception, catch it and
see if this helps you figure out the problem.
These 2 lines look fine assuming that obj is not null and
getArrayList() returns a non-null array list.
2.  Can the contents of an arraylist of strings populate a richtextbox
without iterating through the collection?  I've seen this:  rtb.Text
+= myList.toString() but is there a built-in method which would do
the same thing?

I am not aware of a built-in method, but I would do it something like
this:
StringBuilder sb = new StringBuilder();
foreach (string s in myList) sb.Append(s);
rtb.Text = sb.ToString();
 
C

Chris Shepherd

Adam said:
It doesn't. I've typed the code exactly as it appears in the IDE. I
'm aware that arraylists implements icollection; that is why I used
used AddRange.

Whatever is being returned from your obj.getArrayList() function (which we have
seen no definition for) must not be an actual ArrayList or fails to implement
ICollection at any rate.

The following code compiles and executes just fine for me:

static void Main()
{
ArrayList other = new ArrayList();
other.AddRange(new object[] { "One", 2, "Three", "Four", 5 });

ArrayList al = new ArrayList();
al.AddRange(other);
}

Chris.
 
I

Ignacio Machin ( .NET/ C# MVP )

It doesn't.  I've typed the code exactly as it appears in the IDE.  I
'm aware that arraylists implements icollection; that is why I used
used AddRange.

ArrayList ar = new ArrayList();
ar.Add(1); ar.Add(2); ar.Add(3);
ArrayList ar2 = new ArrayList();
ar2.AddRange(ar);
Console.WriteLine(ar2.Count);

I just tested it and worked fine
 
A

Adam Sandler

ArrayList ar = new ArrayList();
ar.Add(1); ar.Add(2); ar.Add(3);
ArrayList ar2 = new ArrayList();
ar2.AddRange(ar);
Console.WriteLine(ar2.Count);

I just tested it and worked fine

Thanks everyone for their help so far:

Here's everything I have:

In one class here is this:

private ArrayList al = new ArrayList();

public void setWordList(String s)
{
al.Add(s);
}

public ArrayList getWordList()
{
return al;
}

The contents of the arraylist are read from a text file:

public void ReadFromFile(string filename)
{
StreamReader sr;
string s;

sr = File.OpenText(filename);
s = SR.ReadLine();

WordLists wl = new WordLists();

while (s != null)
{
wl.setWordList(a);
s = sr.ReadLine();
}
sr.Close();
}

Yes, I've verified there are items in the arraylist prior to
continuing. Eventually, and based upon what form the user displays, I
want to put the arraylist contents into a richtextbox. So from the
form's constructor, I call this:

public ListEditor()
{
WordLists wl = new WordLists();
ArrayList al = new ArrayList();

al.AddRange(wl.getWordList);
}

The line which reads, al.AddRange(wl.getWordList) has two errors:

Error 1 The best overloaded method match for
'System.Collections.ArrayList.AddRange(System.Collections.ICollection)'
has some invalid arguments
Error 2 Argument '1': cannot convert from 'method group' to
'System.Collections.ICollection'

Again, thanks to everyone for their help thus far
 
T

Tom Shelton

Thanks everyone for their help so far:

Here's everything I have:

In one class here is this:

private ArrayList al = new ArrayList();

public void setWordList(String s)
{
al.Add(s);
}

public ArrayList getWordList()
{
return al;
}

The contents of the arraylist are read from a text file:

public void ReadFromFile(string filename)
{
StreamReader sr;
string s;

sr = File.OpenText(filename);
s = SR.ReadLine();

WordLists wl = new WordLists();

while (s != null)
{
wl.setWordList(a);
s = sr.ReadLine();
}
sr.Close();
}

Yes, I've verified there are items in the arraylist prior to
continuing. Eventually, and based upon what form the user displays, I
want to put the arraylist contents into a richtextbox. So from the
form's constructor, I call this:

public ListEditor()
{
WordLists wl = new WordLists();
ArrayList al = new ArrayList();

al.AddRange(wl.getWordList);
}

The line which reads, al.AddRange(wl.getWordList) has two errors:

You don't have any parens after your call... The method takes an object
of ICollection, but the compiler is going to try and infer this as a
delegate.

On a side note... If these are all strings - you might want to change
your type to List<string> instead of an ArrayList.
 
B

Ben Voigt [C++ MVP]

Adam said:
ARRRRGGGHHH! I hate when that happens. Thanks to everyone for their
help!!!

That's what "cannot convert from 'method group'" means almost 100% of the
time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top