473,657 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to turn ICollection into ArrayList?

since ArrayList implements ICollection, is there a quick way to convert an
ICollection into ArrayList (besides actually iterating through each element
in the ICollection and explicitly adding them to a new ArrayList instance) ??
Nov 16 '05 #1
7 23538
Hi,

Did you ever saw this page?

addrange
http://msdn.microsoft.com/library/de...rangetopic.asp

I hope this helps?

Cor
Nov 16 '05 #2
AddRange simply iterates the collection and copies the items.

MrNobody, I guess the question that I have is, when you refer to "quick",
are you talking about the fewest number of code lines, or the fewest number
of program cycles.

There is no way to automatically turn an ICollection into an ArrayList
without copying the items, unless the collection was originally an
ArrayList. But the simplest way might be to use AddRange like Cor mentioned
below.
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:es******** ******@TK2MSFTN GP11.phx.gbl...
Hi,

Did you ever saw this page?

addrange
http://msdn.microsoft.com/library/de...rangetopic.asp
I hope this helps?

Cor

Nov 16 '05 #3
You can use the AddRange method of the ArrayList to add from an object that
implements an IList interface.

See
http://msdn.microsoft.com/library/de...rangetopic.asp

Shariq Khan
sh****@shariqkh an.com
"MrNobody" <Mr******@discu ssions.microsof t.com> wrote in message
news:FE******** *************** ***********@mic rosoft.com...
since ArrayList implements ICollection, is there a quick way to convert an
ICollection into ArrayList (besides actually iterating through each
element
in the ICollection and explicitly adding them to a new ArrayList instance)
??

Nov 16 '05 #4
MrNobody,
In addition to the AddRange method, I would probably simply use the
ArrayList constructor that accepts an ICollection:

Dim collection As ICollection
Dim list As New ArrayList(colle ction)

For details on this constructor see:

http://msdn.microsoft.com/library/de...ctortopic2.asp

Hope this helps
Jay

"MrNobody" <Mr******@discu ssions.microsof t.com> wrote in message
news:FE******** *************** ***********@mic rosoft.com...
since ArrayList implements ICollection, is there a quick way to convert an
ICollection into ArrayList (besides actually iterating through each
element
in the ICollection and explicitly adding them to a new ArrayList instance)
??

Nov 16 '05 #5
Doh!
I should have given a C# sample :-|

ICollection collection;
ArrayList list = new ArrayList(colle ction);

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:uS******** ******@TK2MSFTN GP15.phx.gbl...
MrNobody,
In addition to the AddRange method, I would probably simply use the
ArrayList constructor that accepts an ICollection:

Dim collection As ICollection
Dim list As New ArrayList(colle ction)

For details on this constructor see:

http://msdn.microsoft.com/library/de...ctortopic2.asp

Hope this helps
Jay

"MrNobody" <Mr******@discu ssions.microsof t.com> wrote in message
news:FE******** *************** ***********@mic rosoft.com...
since ArrayList implements ICollection, is there a quick way to convert
an
ICollection into ArrayList (besides actually iterating through each
element
in the ICollection and explicitly adding them to a new ArrayList
instance) ??


Nov 16 '05 #6
Yeah I was kinda hoping for something that used least cpu cycles- like a type
cast , butt it's ok because I don't really have THAT many items in this
collection... Thanks everyone
Nov 16 '05 #7
Hi,

A shallow copy is the only way of doing it, as ArrayList has its own
internal way to keep the data. Also with a copy the original collection can
be modified afterwards and the arraylist will not be changed.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"MrNobody" <Mr******@discu ssions.microsof t.com> wrote in message
news:BD******** *************** ***********@mic rosoft.com...
Yeah I was kinda hoping for something that used least cpu cycles- like a
type
cast , butt it's ok because I don't really have THAT many items in this
collection... Thanks everyone

Nov 16 '05 #8

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

Similar topics

2
6803
by: Kent Boogaart | last post by:
Hello all, I have two simple classes: Item and ItemCollection. Item stores a label for the item and an instance of ItemCollection for all child items. ItemCollection just stores a collection of Items. ItemCollection implements System.Collections.ICollection. When I use XmlSerializer to serialize an instance of ItemCollection, I am unable to control the element names used to contain the item collection and the items therein. Applying...
7
6535
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection class: Derives MarshalByValueComponent Implements ICollection, IList and ISerializable Explicitly implements the IList methods as private members, (ie. int
8
6677
by: Sam | last post by:
I’m trying to write a Set class where it’s a list that you can add an element only once. So I naturally tried to extend the ArrayList and wrote this code. The problem is that it compiles but during execution it gives this error and points to the “if (!(this.Contains(value)))” line : An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll Here is my program: public class Set :...
9
4312
by: TT ( Tom Tempelaere ) | last post by:
Hi At one point in my application I have a single ArrayList object that I need to break up in two Arraylist objects: the beginning part up to an index, and the ending part from a certain index. I am now using ArrayList.GetRange but the documentation states that this method does not create new ArrayList objects but views into the source ArrayList object ArrayList original // .. ArrayList l1 = original.GetRange( 0, count ) ArrayList l2 =...
6
3163
by: Mathew Yeates | last post by:
I'm baffled by something. I want to turn an ArrayList (which stores strings) into an array of strings. I know I can use the ToArray method and pass in a type. But I can't get the code right. Any help here? Am I supposed to pass in a Type? ArrayList myarr; myarr.Add("foo"); myarr.Add("goo"); string mystrings= myarr.ToArray(??);
7
2917
by: ORC | last post by:
Hi, How to create an ArrayList from a System.Array or inserting the System.Array into the ArrayList ? Thanks Ole
1
5572
by: Sylvain | last post by:
Hi, I'm encountering a very simple issue with ArrayList constructor and AddRange() method overriding. I'm defining a class that extends ArrayList and contains one overriden method: AddRange(ICollection). public class Test:ArrayList {
20
5968
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim mylist as new FrameList mylist.Add(new FrameStructure) mylist(0).first = "blabla..." mylist(0).second...
5
19581
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7321
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.