Solved

Copying OpenForms to array

Posted on 2012-03-14
3
292 Views
Last Modified: 2012-03-23
This should be Really Simple but ... I'm using C# 4.0. I want to copy Application.OpenForms to an array without writing my own loop.

Application.OpenForms is of type System.Windows.Forms.FormCollection. This type does not have a CopyTo method, but http://msdn.microsoft.com/en-us/library/system.windows.forms.formcollection.aspx says that ICollection.CopyTo is an explicit interface implementation. I don't see how to go about using that.
0
Comment
Question by:BlearyEye
3 Comments
 
LVL 40
ID: 37723393
You cannot use the CopyTo mentionned in that web page.

If you look carefully, there is not hyperlink from the ICollection.CopyTo method. Only an indication that it is inherited from ReadOnlyCollectionBase. If you follow that lead, you see that the method is Private. That means that is it only used internally by ReadOnlyCollectionBase and cannot be used. When you see a list of methods under a heading of Explicit Interface Implementations, it is an indication for programmers who would want to inherit from the class, not things that you can use from an instance of the class.

Since Microsoft has not built a Copy method into the class, you have no choice, you need to loop. What is wrong with that? It requires only 3 lines of code.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 37723968
use the following:
var forms = Application.OpenForms.Cast<System.Windows.Forms.Form>().ToArray();

and if u insist on CopyTo:
var forms = Application.OpenForms.Cast<System.Windows.Forms.Form>().ToArray().CopyTo(...
0
 
LVL 1

Author Closing Comment

by:BlearyEye
ID: 37758848
Looks good, thanks.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Capture logon name 13 70
Reference issues in Visual Studio 3 29
Hey!! 5 33
Send SMS from VB via Sierra Wireless Modem 2 21
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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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