By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,246 Members | 1,813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,246 IT Pros & Developers. It's quick & easy.

Elegant way to convert Collection to Array[]

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.