Solved

Copying OpenForms to array

Posted on 2012-03-14
3
302 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
[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 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

Technology Partners: 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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 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