Don't shoot me, I'm new to this as well, but would this be what you want?

1
2
3
4
5
6
7
8
9
10
11
open System.Windows.Forms


let objControls = [|box (new Button()); box (new ListBox()); box (new Label())|]


let controls = objControls |> Array.map (fun oc -> oc :?> Control)


let form = new Form()
form.Controls.AddRange(controls)

Where objControls is your array of objects-that-are-actually-controls.

By on 4/23/2009 1:49 AM ()

I believe the best you can do is use the upcast keyword:

1
let arr : Controls array = [| upcast b; upcast c; upcast d; |]
By on 4/22/2009 5:23 PM ()

Seq.cast works fine for me:

> [""] |> Seq.cast<System.Object>;;
val it : seq<System.Object> = seq [""]

or:

> let tmp : System.Object seq = [""] |> Seq.cast;;

val tmp : seq<System.Object>

As I understand it, Seq.cast does a downcast, but it seems to cover upcasts as well. You might get null pointer exceptions at run time in the case where the types aren't correctly related, though.

I wonder, would it be hard to make a type-safe Seq.upcast?

By on 4/23/2009 1:06 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper