Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Inherit from C# class + access protected fields

I have this base class that creates a new SQL Server connection and does some utility methods in C#, I want to inherit in F#. Currently I cannot access the protected fields in the C# class from F#, although this would work in C#.

C# abstract class

public abstract class SQL
{
    protected readonly SqlConnectionStringBuilder bld;
    protected readonly SqlCommand cmd;
    protected readonly SqlConnection conn;
    protected readonly string connstring;
    protected string userid;

    public SQL(string server, string db)
    {
        bld = new SqlConnectionStringBuilder();
        bld.DataSource = server;
        bld.InitialCatalog = db;
        bld.IntegratedSecurity = true;
        connstring = bld.ConnectionString;

        conn = new SqlConnection(connstring);
        cmd = new SqlCommand();

        GetUserID();
        //Other utility methods here
    }

F# code to inherit

type Transfer ( server : string ) ( db : string ) = 
inherit SQL(server, db)  
let accessAbstractConn = conn. //Can't access protected field????

Have I missed something here? I have tried aliasing the base class field e.g. this.conn which also does not work.

Thanks Richard

like image 765
Richard Todd Avatar asked Dec 12 '22 17:12

Richard Todd


1 Answers

You can add a self-identifier to the constructor:

type Transfer ( server : string, db : string ) as this = 
  inherit SQL(server, db)  
  let accessAbstractConn = this.conn

or, use the base keyword:

type Transfer ( server : string, db : string ) = 
  inherit SQL(server, db)  
  let accessAbstractConn = base.conn
like image 200
Daniel Avatar answered Dec 20 '22 08:12

Daniel