472,952 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 software developers and data experts.

2 questions about array lists

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[i].toString() but is there a built-in method which would do
the same thing?

Thanks,

--Chris
Jun 27 '08 #1
9 1257
On Apr 24, 11:04*am, Adam Sandler <cor...@excite.comwrote:
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
Jun 27 '08 #2
On Apr 24, 9:18*am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
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.
Jun 27 '08 #3
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[i].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();
Jun 27 '08 #4
Adam Sandler wrote:
On Apr 24, 9:18 am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
>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.
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.
Jun 27 '08 #5
On Apr 24, 11:23*am, Adam Sandler <cor...@excite.comwrote:
On Apr 24, 9:18*am, "Ignacio Machin ( .NET/ C# MVP )"

<ignacio.mac...@gmail.comwrote:
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.
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
Jun 27 '08 #6
On Apr 24, 12:27 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Apr 24, 11:23 am, Adam Sandler <cor...@excite.comwrote:
On Apr 24, 9:18 am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
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.

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.Coll ections.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
Jun 27 '08 #7
On 2008-04-24, Adam Sandler <co****@excite.comwrote:
On Apr 24, 12:27 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
>On Apr 24, 11:23 am, Adam Sandler <cor...@excite.comwrote:
On Apr 24, 9:18 am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
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.

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:
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<stringinstead of an ArrayList.

--
Tom Shelton
Jun 27 '08 #8
On Apr 24, 3:27 pm, Tom Shelton
<tom_shel...@YOUKNOWTHEDRILLcomcast.netwrote:
You don't have any parens after your call...
ARRRRGGGHHH! I hate when that happens. Thanks to everyone for their
help!!!
Jun 27 '08 #9
Adam Sandler wrote:
On Apr 24, 3:27 pm, Tom Shelton
<tom_shel...@YOUKNOWTHEDRILLcomcast.netwrote:
>You don't have any parens after your call...

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.
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Colin J. Williams | last post by:
numarray is a package which is under development and intended to replace Numeric, an efficient and operational package. One of the classes in numarray is NumArray. As currently implemented,...
2
by: bearophile | last post by:
Hello, I have few more things to say/ask (left from a discussion in another Python Newsgroup). Is it possibile (and useful) to write few small sub-sections of the Python interpreter in Assembly...
1
by: Aaron Lovi | last post by:
Hi, I'm a noob trying to use managed DirectX (VC# 2003, DX9.0 Apr 2005 SDK). I used to use OpenGL with QT and VC++6.0, but I was a relative newcomer to that as well. When I used OpenGL, my...
27
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
7
by: alternativa | last post by:
Hello, I have a few questions concerning classes. 1) Why some people use default constructos, i.e constructors with no parameters? To me it doesn't make any sense, is there something I should...
5
by: bruce | last post by:
hi... i'm trying to deal with multi-dimension lists/arrays i'd like to define a multi-dimension string list, and then manipulate the list as i need... primarily to add lists/information to the...
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
16
by: marc_r_bertrand | last post by:
To all asp/db pros: The quiz code below works. But there is a problem when too many questions are answered (radio buttons clicked). I am not an asp pro. So, is there a pro out there or an...
5
by: Tobiah | last post by:
I checked out the array module today. It claims that arrays are 'efficient'. I figured that this must mean that they are faster than lists, but this doesn't seem to be the case: ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.