Silent3241 Silent3241 - 1 month ago 10
C# Question

F# Implement base class

I'm new to F# and I wanted to create an F# Forms Application. But the Problem that I have at the moment, is that I don't know how to implement a base class. In C# I would do it like that

namespace Test_Form
{
public class Form1 : Form
{
public Form1()
{

}
}
}


But how do I make it in F# so that my Forms is a module and not a type

I already tryed

open System
open System.Windows.Forms

type Form1() =
inherit Form()


but that would result in

public class Form1 : Form
{
public Form1() : this()
{
}
}

Answer

Where you would use the constructor method in C#

public class Form1 : Form
{
    public Form1()
    {
        // Do stuff
    }
}

In F#, quoting the docs,

The do-bindings section includes code to be executed upon object construction.

you would

type Form1() =
    inherit Form()

    do // stuff

For example:

type Form1() as this =
    inherit Form()

    do this.Click.Add(fun args -> printfn "%s" <| args.ToString())
Comments