Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

Copying OpenForms to array

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
BlearyEye
Asked:
BlearyEye
1 Solution
 
Jacques Bourgeois (James Burger)Commented:
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
 
Meir RivkinFull stack Software EngineerCommented:
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
 
BlearyEyeAuthor Commented:
Looks good, thanks.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now