473,499 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1275
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
1453
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
1342
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
2011
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
3001
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
1746
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
2267
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
3727
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
1700
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
2182
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
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.