ZozV
ZozV

Reputation: 80

Protected Access

I'm currently extending a project someone else worked on and I have been trying to get the information on a variable that is protected. I tried several things already, but the variable is always null.

public class AController : Controller
{
    Protected Login _login {get; set;}
}
public class BController : AController
{
    _login = login;
}
public class CController : AController
{
    if(_login != null)
    {      
    }
}

This is the way I thought it would work. I know I can't do CController : BController since it's protected. Is there maybe another way to get the information out of BController to CController.

Upvotes: 1

Views: 62

Answers (2)

Aurimas Neverauskas
Aurimas Neverauskas

Reputation: 788

So you have instance of BController and instance of CController, both inheriting AController. So they're two different instances when you create them and each of them has it's own _login variable, changing one, won't change another, unless that variable is static and shared across instances.

You should use static keyword on AController's property _login to achieve your desired result.

However the right solution would be to pass login as constructor parameter to your BController and CController constructors so they would share same instance of login.

If these controllers are MVC controllers, why would you need to instantiate them both on same request?

Upvotes: 3

Aurel
Aurel

Reputation: 13

I'm not sure about what i'm saying, but to access these data don't your object need to inherit from your superclass ?

Upvotes: 0

Related Questions