Solved

how to convert ComponentCollection to Component[].

Posted on 2010-08-30
10
488 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 500 total points
ID: 33570299
JensMig  - You're right. My mistake.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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