Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to convert ComponentCollection to Component[].

Posted on 2010-08-30
10
Medium Priority
?
493 Views
Last Modified: 2013-12-17
Hi,

I have Class Component.

I have ComponentCollection class

I am trying to convert my ComponentCollection to Component[].

Could you please say me how to convert ComponentCollection to Component[].
0
Comment
Question by:kravindra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 16

Expert Comment

by:Stephan
ID: 33564465
Here is an example how to convert it into an array

//the array
System.ComponentModel.IComponent[] comp;
//the collection
System.ComponentModel.ComponentCollection coll = new System.ComponentModel.ComponentCollection();

//coll copies to comp with a starting index of 0
coll.CopyTo(comp, 0);
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 33564624
Hi,
You can just run a for loop to do so.
ComponenentCollection collection;
int length= collection.Count;
Component[] compArray= new Component[length];
for(int i=0i<length;i++)
{
compArray[i]= collection[i];
}
This is the simplest way to do so..
You can use
collection.CopyTo(compArray) or
compArray = collection.ToArray() ; //if ComponentCollection is generic collection or
compArray= collectio.ToArray(typeof(Component));
 
but both these methods(CopyTo and ToArray) are also doing same thing as, ToArray() calls Array.Copy() method and Array.Copy calls
 
[MethodImpl(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
internal static extern void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable);
 which is again doing the same sequential copy.
 
Please let me know if you need further information.

public void CopyTo(T[] array, int index)
{
    if (array == null)
    {
        throw new ArgumentNullException("array");
    }
    if ((index < 0) || (index > array.Length))
    {
        throw new ArgumentOutOfRangeException("index", SR.GetString("IndexOutOfRange", new object[] { index }));
    }
    if ((array.Length - index) < this.Count)
    {
        throw new ArgumentException(SR.GetString("Arg_InsufficientSpace"));
    }
    LinkedListNode<T> head = this.head;
    if (head != null)
    {
        do
        {
            array[index++] = head.item;
            head = head.next;
        }
        while (head != this.head);
    }
}

Open in new window

0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33569828
You need reference to System.Xml.Linq assembly


ComponentCollection cc = new ComponentCollection(new IComponent[] { });
            IEnumerable<IComponent> components = cc.Cast<IComponent>();

Open in new window

0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 1

Expert Comment

by:JensMig
ID: 33570032
If you use LINQ (that means .NET 3.5 or 4.0), you can do it in one simple, single line:


System.ComponentModel.Component[] myArray = componentCollection.OfType<System.ComponentModel.Component>().ToArray();

Open in new window

0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 33570061
JensMig its almost same as

System.ComponentModel.Component[] myArray = componentCollection.ToArray();

and it is also using Array.CopyTo internally....

so no benifit...

0
 
LVL 1

Expert Comment

by:JensMig
ID: 33570175
Well, System.ComponentModel.ComponentCollection does not have a Method ToArray.
And CopyTo needs more lines than neccessary.
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33570193
JensMig - Adding reference to System.Xml.Linq will get those extension methods.
0
 
LVL 1

Expert Comment

by:JensMig
ID: 33570250
Novice Novice - No, you do not get a ToArray method on the Collection itself. Please try it out yourself. You only get a ToArray if you cast the collection items - which OfType() does.

If I am wrong, please say so and show me how to call ToArray directly.
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 33570253
yes...

but CopyTo is there and its more than simple..

but its same as any other method as it will also call Array.Copy...

System.ComponentModel.ComponentCollection

public void CopyTo(IComponent[] array, int index)
{
    base.InnerList.CopyTo(array, index);
}

 
Here InnerList is simple ArrayList


public virtual void CopyTo(Array array, int arrayIndex)
{
    if ((array != null) && (array.Rank != 1))
    {
        throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
    }
    Array.Copy(this._items, 0, array, arrayIndex, this._size);
}

 
0
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 2000 total points
ID: 33570299
JensMig  - You're right. My mistake.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question