Hi, I just answered another question regarding the class syntax, so you may want to check it out here: [link:cs.hubfs.net].

Inheriting from types without parameterless constructor is possible using the explicit syntax (your MyForm1) as well. You just have to call the base constructor in your constructor (eg. new() = { inherit Form(123); a = 10 })

In general, the first syntax is there if you need to define some class in the same way as you'd do in C#. It allows writing a few things that cannot be done using the second syntax and it is useful for example for CodeDOM, but there are really only a few things that cannot be done using the implicit syntax, so the implicit syntax is preferred, unless you really need something special. Your second example can be written more nicely like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
type MyForm2() as t = // 't' is 'this' reference 
    inherit Form()
    let button2 = new Button()    
    // local function declaration (not visible from outside of the class)
    let foo () = 
      10     
    // initialization in the 'constructor'
    do 
      t.Controls.Add button2
    member x.Foo () = foo () // Usual public member - can use local functions
    
let form2 = new MyForm2()
form2.Show()
By on 9/6/2008 6:43 AM ()

Thanks

Because when I learn F# this year. I have not learned C# yet (and the other OO language). So I don't know how to play with Class before.

I know how to do something in implicit syntax

type MyClass() =

let _ = print_any 1

do print_any 2

But I don't know how to access itself (as xxx || base.xxx())

Thanks all of you! I learn so many new ways to play with Class.

By on 9/7/2008 5:22 PM ()

You could also use the new keyword base so:

1
2
3
4
type MyForm2() =
    inherit Form()
    let button2 = new Button()
    do base.Controls.Add button2

regards,
Danny

By on 9/6/2008 7:45 AM ()

Danny where is the new keyword base documented?
[link:research.microsoft.com]?

Thanks, art

By on 9/6/2008 12:05 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