473,414 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Elegant way to convert Collection to Array[]

How can I convert (elegant way) Collection to Array without looping and new
instances.
E.G:
I want to add elements of one menu to other, so
secondMenu.MenuItems.AddRange(firstMenu.MenuItems) ; //Error here: Argument
'1': cannot convert from 'System.Windows.Forms.Menu.MenuItemCollection' to
'System.Windows.Forms.MenuItem[]'

So please advice.
Nov 16 '05 #1
4 50738
Not just in menu (not to use MergeMenu in this case ;) )

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
How can I convert (elegant way) Collection to Array without looping and new instances.
E.G:
I want to add elements of one menu to other, so
secondMenu.MenuItems.AddRange(firstMenu.MenuItems) ; //Error here: Argument
'1': cannot convert from 'System.Windows.Forms.Menu.MenuItemCollection' to
'System.Windows.Forms.MenuItem[]'

So please advice.

Nov 16 '05 #2
The ICollection interface provides a "CopyTo" method that copies the
collection data into an array. However, you can be pretty sure it does this
with a loop.

There is no generic to "convert" a collection to an array that does not
involve copying the elements of the collection: The collection could contain
anything, from a linked list to a database recordset; The only way to
convert these to an array is to copy them element by element, as the memory
layout of an array is completely different from say, a linked list.

Niki

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in
news:%2****************@TK2MSFTNGP12.phx.gbl...
How can I convert (elegant way) Collection to Array without looping and new instances.
E.G:
I want to add elements of one menu to other, so
secondMenu.MenuItems.AddRange(firstMenu.MenuItems) ; //Error here: Argument
'1': cannot convert from 'System.Windows.Forms.Menu.MenuItemCollection' to
'System.Windows.Forms.MenuItem[]'

So please advice.

Nov 16 '05 #3
The ArrayList class can be instantiated with an ICollection instance (which
is quite elegant).
You could then convert the ArrayList to a regular Array using the ToArray
function.
The Array object doesn't have any mechanism to instantiate or fill itself
from an ICollection instance.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
How can I convert (elegant way) Collection to Array without looping and new instances.
E.G:
I want to add elements of one menu to other, so
secondMenu.MenuItems.AddRange(firstMenu.MenuItems) ; //Error here: Argument
'1': cannot convert from 'System.Windows.Forms.Menu.MenuItemCollection' to
'System.Windows.Forms.MenuItem[]'

So please advice.

Nov 16 '05 #4
To be clear: ToArray can be used like this:

MyClass[] myClassArray = (MyClass[])myArrayList.ToArray(typeof(MyClass));

Christian

"John Wood" wrote:
The ArrayList class can be instantiated with an ICollection instance (which
is quite elegant).
You could then convert the ArrayList to a regular Array using the ToArray
function.
The Array object doesn't have any mechanism to instantiate or fill itself
from an ICollection instance.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
How can I convert (elegant way) Collection to Array without looping and

new
instances.
E.G:
I want to add elements of one menu to other, so
secondMenu.MenuItems.AddRange(firstMenu.MenuItems) ; //Error here: Argument
'1': cannot convert from 'System.Windows.Forms.Menu.MenuItemCollection' to
'System.Windows.Forms.MenuItem[]'

So please advice.


Nov 16 '05 #5

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

Similar topics

4
by: Dan | last post by:
I need to convert a byte array to a string in order to upload a binary file with an httpWebRequest. What's the most efficient way to do such a conversion?
2
by: Dave | last post by:
Hi, I'm trying to convert a byte array to string --This works... System.BitConverter.ToString(bytes) "EB-55-79-20-18-B2-76-4D-85-0A-93-6B-97-33-31-B8" --This doesn't, but returns...
2
by: wh | last post by:
I have a user control implemented in an .ascx page that I embed in the html as follows: <test:mycontrol runat="server" property1="..." property2="..."/> I'd also like to be able to assign a...
1
by: Elioth | last post by:
Hi... I need to know how to convert Char Array to Byte Array and vice-versa in VB 2K5 Thanks for all help. Elioth
7
by: SpreadTooThin | last post by:
I have some code... import array a = array.array('d') f = open('file.raw') a.fromfile(f, 10) now I need to convert them into floats (32 bit...) what do i do?
2
by: sookfuny1984 | last post by:
i would like to know on how to convert an array so that the variable can be used to store in database table...
8
by: Frank Liebelt | last post by:
Hi I try to convert a int array into a char array. My code: void exec() { char mds; int i; int mdc =...
2
by: David | last post by:
Hey can anyone help me convert a javascript array into a ruby array. Ive been struggling with this since friday to no avail. This is the function with the ajax.request call that is supposed to...
18
by: MrVS | last post by:
Hi, I have a C++ CLR class method that takes System::Byte *b as parameter argument. I want the CSharp caller pass a byte * to this function. But in the CSharp prorgram, I only managed to create a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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
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...
0
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...

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.