[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to convert ComponentCollection to Component[].

Posted on 2010-08-30
10
Medium Priority
?
494 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

873 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