Solved

how to convert ComponentCollection to Component[].

Posted on 2010-08-30
10
476 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now