Link to home
Create AccountLog in
Avatar of BlearyEye
BlearyEyeFlag for United States of America

asked on

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.
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

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.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of BlearyEye

ASKER

Looks good, thanks.