Naveen Verma
Naveen Verma

Reputation: 387

Declaring a object public and then using it locally in F#

I don't know much about F# but i am working on a project (on C#) in which i have to make few changes inside pre-written F# code.

I want to declare a object public and then use this object locally.(like we do in C#).

class abc
{
   public SubscriptionManager subman = null;  // object of class SubscriptionManager

   public void DoSomething()
   {
      subman = new SubscriptionManager();
   }
}

Something like this.

Can i do this same thing into the F#?

Upvotes: 2

Views: 81

Answers (1)

Anton Schwaighofer
Anton Schwaighofer

Reputation: 3149

As people mentioned in the comments, there are a couple of reasons why this may not be the right thing to do. That being said, this code would give you roughly what you are after:

[<AllowNullLiteral>]
type Manager() = class end

type abc() = 
    member val subman: Manager = null with get, set
    member this.DoSomething() = this.subman <- Manager()

I've included a dummy Manager class just to make the thing compile. There's a related discussion about public fields also here, that lists a solution where at least the mutable fields is not public.

Upvotes: 5

Related Questions