Solved

how to convert ComponentCollection to Component[].

Posted on 2010-08-30
10
486 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
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Easy filter aspnet 2 34
Reactjs with .NET 3 70
How to repeat the data 4 30
Asp.Net Session Question 2 33
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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