Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access protected member

Tags:

protected

f#

I have following code in extending type (in F#) which invokes a protected method of a class it inherits from (in C#) but I get the exception (see below). Is there a workaround for this?

let getPagereference id =
    this.ConstructPageReference(id)

The member or object constructor 'ConstructPageReference' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.

Update:

I have tried following but getting the same result

let getPagereference id =
    base.ConstructPageReference(id)

Update 2 (solution):

here is the code as it was:

type MyNewType() =
    inherit SomeAbstractType()

    let getPagereference id =
        base.ConstructPageReference(id)

    override this.SomeMethod()=
       let id = 0
       let pr = getPagereference id

this is how it should have been:

type MyNewType() =
    inherit SomeAbstractType()

    member this.ConstructPageReference(id) =
        base.ConstructPageReference(id)

    override this.SomeMethod()=
       let id = 0
       let pr = this.ConstructPageReference(id)
like image 940
Enes Avatar asked Mar 02 '10 20:03

Enes


People also ask

How can a protected member be accessed in Java?

Protected Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces.

Can protected members be accessed by objects?

Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class. For related information, see friend, public, private, and the member-access table in Controlling Access to Class Members.

How do I access protected classes?

We can access protected members of a class in its subclass present in a different package. In the following example, we will create two classes. Sample class in package1 and Child class in package2. Child class extends Sample class.

Can we access protected member outside the package?

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.


2 Answers

Gabe is correct. Your code:

let getPagereference id =
  this.ConstructPageReference(id)

is the same as

let getPagereference = fun id ->
  this.ConstructPageReference(id)

and you are therefore implicitly attempting to call a base method from within a lambda expression. You will need to do this from a member, rather than a let-bound function.

like image 76
kvb Avatar answered Sep 27 '22 01:09

kvb


I bet the key part is the cannot be accessed from inner lambda expressions. You are probably trying to do the access from within a lambda.

Have you tried

member this.getPagereference(id) = 
    this.ConstructPageReference(id) 
like image 40
Gabe Avatar answered Sep 24 '22 01:09

Gabe